Downloading and Installing Node.js

To install Node.js, follow these steps:

  1. Download Node.js

  2. Install Node.js

  3. Verify Installation

    Open a terminal or command prompt and run:

    node -v   # Check Node.js version
    npm -v    # Check npm version
    

Global Variables in Node.js

In Node.js, global variables are accessible from anywhere in the application without requiring explicit imports. However, unlike browsers, Node.js does not have a global window object. Instead, it uses the global object.

Common Global Variables in Node.js

  1. global – The root object that holds all global variables.

    console.log(global); // Displays all global properties
    
  2. _dirname – Returns the directory path of the current module.

    console.log(__dirname); // Outputs: /Users/example/project
    
  3. __filename – Returns the absolute path of the current file.

    console.log(__filename); // Outputs: /Users/example/project/index.js
    
  4. process – Provides information about the Node.js process.

    console.log(process.version); // Outputs Node.js version
    
  5. console – A built-in debugging tool (similar to console.log in browsers).

    console.log("Hello, Node.js!"); // Outputs: Hello, Node.js!
    
  6. setTimeout(callback, delay) – Executes a function after a specified delay.

    setTimeout(() => console.log("Executed after 2 seconds"), 2000);
    
  7. setInterval(callback, interval) – Repeats execution at fixed intervals.

    setInterval(() => console.log("Runs every 3 seconds"), 3000);
    
  8. require – Used to import modules in CommonJS.

    const fs = require("fs"); // Imports file system module
    

JavaScript is Functional in Nature

JavaScript is often referred to as a functional programming language because it treats functions as first-class citizens. This means: