To install Node.js, follow these steps:
Download Node.js
Install Node.js
Verify Installation
Open a terminal or command prompt and run:
node -v # Check Node.js version
npm -v # Check npm version
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.
global – The root object that holds all global variables.
console.log(global); // Displays all global properties
_dirname – Returns the directory path of the current module.
console.log(__dirname); // Outputs: /Users/example/project
__filename – Returns the absolute path of the current file.
console.log(__filename); // Outputs: /Users/example/project/index.js
process – Provides information about the Node.js process.
console.log(process.version); // Outputs Node.js version
console – A built-in debugging tool (similar to console.log in browsers).
console.log("Hello, Node.js!"); // Outputs: Hello, Node.js!
setTimeout(callback, delay) – Executes a function after a specified delay.
setTimeout(() => console.log("Executed after 2 seconds"), 2000);
setInterval(callback, interval) – Repeats execution at fixed intervals.
setInterval(() => console.log("Runs every 3 seconds"), 3000);
require – Used to import modules in CommonJS.
const fs = require("fs"); // Imports file system module
JavaScript is often referred to as a functional programming language because it treats functions as first-class citizens. This means: