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, 2Blocking vs. Non-Blocking I/O
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 BExercise: 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.