Node.js is a JavaScript runtime built on Chrome's V8 engine. Unlike browser JavaScript, Node.js lets you run JS on a server — accessing the file system, network, and OS directly.
The Event Loop
Node.js is single-threaded but handles thousands of concurrent operations via the event loop. Instead of blocking while waiting for I/O (file reads, network requests), Node.js registers a callback and moves on. When the I/O completes, the callback is placed on the event queue and executed.
// Synchronous — blocks the thread until complete
const data = fs.readFileSync('file.txt', 'utf8')
console.log(data) // waits here
// Asynchronous — registers callback, continues immediately
fs.readFile('file.txt', 'utf8', (err, data) => {
// ...process and global objects
Node.js provides process (current process info, env vars, exit codes) and global (equivalent of window in browsers).
console.log(process.version) // Node.js version
console.log(process.env.HOME) // environment variable
process.exit(0) // exit cleanlyNode.js is a JavaScript runtime built on Chrome's V8 engine. Unlike browser JavaScript, Node.js lets you run JS on a server — accessing the file system, network, and OS directly.
The Event Loop
Node.js is single-threaded but handles thousands of concurrent operations via the event loop. Instead of blocking while waiting for I/O (file reads, network requests), Node.js registers a callback and moves on. When the I/O completes, the callback is placed on the event queue and executed.
// Synchronous — blocks the thread until complete
const data = fs.readFileSync('file.txt', 'utf8')
console.log(data) // waits here
// Asynchronous — registers callback, continues immediately
fs.readFile('file.txt', 'utf8', (err, data) => {
// ...process and global objects
Node.js provides process (current process info, env vars, exit codes) and global (equivalent of window in browsers).
console.log(process.version) // Node.js version
console.log(process.env.HOME) // environment variable
process.exit(0) // exit cleanly