Node.js Basic Syntax and Features

JavaScript Basics:

Variables: are used to store data values.

They can be declared using var, let, or const.

                                                    
                                                    var age = 30;
let name = "John";
const PI = 3.14;
                                                    
                                                

Data type specifies the type of data that a variable can hold.

Primitive types: number, string, boolean, null, undefined

Complex types: object, array, function

                                                    
                                                    let num = 10;
let str = "Hello";
let bool = true;
let arr = [1, 2, 3];
let obj = { name: "John", age: 30 };
                                                    
                                                

Functions are blocks of reusable code that can be called with arguments and return values.

They can be defined using the function keyword or using arrow functions () => {}.

                                                    
                                                    function greet(name) {
    return "Hello, " + name + "!";
}

let add = (a, b) => {
    return a + b;
};
                                                    
                                                

Node.js Specific Features:

Global Objects: There are several global objects in Node.js modules.

Some common ones include:

  • console: This logs messages to the console.
  • process: It is used to provides details on the present Node.js process.
  • Buffer: It is used in dealing with binary data.

Modules: Node.js has a module system based on CommonJS.

Modules are individual pieces of code that can be exported and imported using require and module.exports respectively.

                                                    
                                                    // greet.js
function greet(name) {
    return "Hello, " + name + "!";
}

module.exports = greet;
                                                    
                                                

                                                    
                                                    // app.js
const greet = require('./greet');

console.log(greet("John")); // Output: Hello, John!
                                                    
                                                

Events: In Node.js, an architecture built on events is used where certain emitters emit named events that trigger associated listeners to be called up on by related functions.

The built-in EventEmitter class handles events .Example:

                                                    
                                                    const EventEmitter = require('events');

class MyEmitter extends EventEmitter {}

const myEmitter = new MyEmitter();

myEmitter.on('event', () => {
    console.log('An event occurred!');
});

myEmitter.emit('event'); // Output: An event occurred!

                                                    
                                                

Callbacks: Callbacks abound in async programming in Node.js.

A callback refers to a function passed as a parameter to another function for execution later after completion of certain activities. Example:

                                                    
                                                    function fetchData(callback) {
    setTimeout(() => {
        callback("Data fetched successfully");
    }, 2000);
}

fetchData((data) => {
    console.log(data); // Output: Data fetched successfully
});