NodeJs Basics Practice
NodeJs Basics Practice
NodeJs Basics Practice
let x = 10;
const y = 20;
3. Template Literals:
Template literals allow for easier string interpolation.
4. Destructuring:
Destructuring assignment allows for unpacking values from arrays or
properties from objects into distinct variables.
5. Default Parameters:
Functions can have default values for parameters.
function sum(...numbers) {
return numbers.reduce((acc, curr) => acc + curr, 0);
}
7. Classes:
ES6 introduced a more straightforward and concise way to create classes and
handle inheritance.
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log(`Hello, my name is ${this.name}.`);
}
}
8. Promises:
Promises provide a cleaner way to handle asynchronous operations.
ES6 Modules:
ES6 modules allow you to export and import code between different files, making
it easier to organize and maintain your codebase.
Exporting:
// In file `math.js`
export const add = (a, b) => a + b;
export const subtract = (a, b) => a - b;
Importing:
// In file `main.js`
import { add, subtract } from './math.js';
console.log(add(5, 3)); // 8
console.log(subtract(5, 3)); // 2
If you have specific questions or need examples on any of these features, feel
free to ask!
Node.js Environment:
Non-Blocking I/O: Node.js was designed for building scalable network
applications and uses non-blocking I/O operations to handle many
connections simultaneously.
1. Event Loop: The event loop is a core feature of Node.js that allows it to
perform non-blocking I/O operations. It listens for events and executes the
corresponding callback functions.
The event loop continuously checks for pending tasks (e.g., timers,
network requests, file I/O).
2. Events and Callbacks: In Node.js, many APIs are designed to emit events. For
example, the http server emits events when a request is received.
In this example, the createServer method sets up an event listener for incoming
HTTP requests.
Summary:
const os = require('os');
const os = require('os');
console.log('Platform:', os.platform());
// Possible outputs: 'linux', 'darwin', 'win32'
const os = require('os');
const os = require('os');
const os = require('os');
const os = require('os');
7. Getting Hostname:
const os = require('os');
console.log('Hostname:', os.hostname());
// Outputs the hostname of the operating system
const os = require('os');
const os = require('os');
const os = require('os');
const os = require('os');
These snippets demonstrate how to use the os module to gather various pieces
of information about the operating system and hardware, which can be very
useful for system monitoring, diagnostics, and optimization tasks.