Learn Node.js

Node.js lets you run JavaScript outside the browser using an event-driven, non-blocking runtime built for fast, scalable network applications.

What Is Node.js?

Node.js is an open-source, cross-platform JavaScript runtime built on Google's V8 engine, the same engine that powers Chrome. Released in 2009 by Ryan Dahl, it lets developers execute JavaScript on a server, a desktop, or the command line instead of only inside a browser tab. Because it shares one language with the front end, teams can move code and skills between client and server with almost no friction.

Why Developers Use Node.js

  • Full-stack JavaScript — write the browser and the server in one language.
  • A single-threaded event loop that serves thousands of concurrent clients without spawning a thread per request.
  • npm, the world's largest package registry, giving instant access to reusable modules.
  • A small memory footprint and fast startup, which suits microservices, CLIs, and real-time apps.

The Event Loop and Non-Blocking I/O

Node.js runs your JavaScript on a single main thread, but it never waits idly for slow operations like disk reads or network calls. Those tasks are handed off to the operating system or a background thread pool (via a library called libuv), and Node keeps executing other code. When the operation finishes, its callback is queued and the event loop picks it up once the current work is done.

Ordering the Event Loop

console.log("1: script start");

setTimeout(() => {
  console.log("2: timeout callback");
}, 0);

Promise.resolve().then(() => {
  console.log("3: promise callback");
});

console.log("4: script end");

// Actual output order: 1, 4, 3, 2
Note: Promises resolve in the 'microtask queue', which Node drains completely before it moves on to timers, I/O callbacks, or other 'macrotasks'. That's why the promise callback above logs before the setTimeout callback, even though both were scheduled with a near-zero delay.

Blocking vs. Non-Blocking I/O

OperationBlocking (synchronous)Non-blocking (asynchronous)
Reading a filefs.readFileSync halts everything until the disk respondsfs.readFile keeps the event loop free and calls you back when data arrives
Querying a databaseThe thread sits idle until the query returnsThe driver returns a promise/callback and the loop serves other requests meanwhile
Handling many clientsEach blocked request delays every other requestThousands of requests can be in flight because none of them monopolize the thread

A Blocking Operation Freezes Everything

function blockFor(ms) {
  const end = Date.now() + ms;
  while (Date.now() < end) {
    // busy-wait: this loop occupies the single thread completely
  }
}

console.log("Start");
setTimeout(() => console.log("Delayed timer fires late"), 100);
blockFor(2000); // freezes the entire process for 2 seconds
console.log("End of synchronous work");

// The timer set for 100ms doesn't fire until after blockFor() finishes,
// because nothing else can run while the thread is busy.

Non-Blocking Code Keeps Moving

console.log("Task A: begins");

setTimeout(() => {
  console.log("Task B: runs later, without blocking Task C");
}, 500);

console.log("Task C: runs immediately, right after Task A");

// Output: Task A, Task C, Task B
Note: Because Node.js is single-threaded, a long synchronous computation (a huge loop, heavy JSON parsing, complex regex) blocks every other request. For CPU-intensive work, offload it to the worker_threads module or a separate process so the event loop stays responsive.

Exercise: Node.js Introduction

What is Node.js, fundamentally?

Frequently Asked Questions

What is Node.js used for?
Running JavaScript outside the browser, most often for web servers, APIs and command-line tools. It lets a team use one language across both the front-end and the backend.
Is Node.js a programming language?
No. It is a runtime that executes JavaScript on a server, built on Chrome's V8 engine. The language you write is still JavaScript.
What is the difference between Node.js and the browser?
Node has file system, network and process access but no DOM or window object. Browser JavaScript has the DOM but no direct file access. Code using one set will not run in the other.