01 / 21

Browser vs. Node.js

Discover how the same language runs in two very different worlds: the browser tab and the server terminal.

JavaScript

What Is JavaScript?

JavaScript is a programming language originally created to make web pages interactive. Today it powers everything from animated buttons to entire backend servers.

Think of it like speaking English: the language is the same whether you are in a classroom or on a phone call, but the tools available to you differ.

What Is a Runtime Environment?

A runtime environment is the place where your code actually runs. It provides the engine that reads JavaScript and the APIs (built-in tools) your code can call.

JS Engine
Parses and executes your code. Many popular browsers (like Chrome) and Node.js are built on Google's powerful V8 engine.
APIs
Extra capabilities supplied by the host: DOM manipulation in browsers, file-system access in Node.js.
Event Loop
A mechanism that handles asynchronous tasks. Present in both runtimes, but with different backing libraries.

Two Runtimes, One Engine

V8 Engine Browser Runtime window, document DOM API fetch, localStorage alert, console Web APIs (libuv-like) No fs, no process Node.js Runtime global, process fs (file system) http, path, os Buffer, console libuv event loop No DOM, no window

The Core Rule

Browser Gives You
window, document, DOM manipulation, fetch, localStorage, Web Audio, Canvas, etc.
Node.js Gives You
global, process, fs (file system), http, path, os, Buffer, etc.

If your code only uses pure JS features (like Array.map or Promise.all), it runs in both runtimes. The moment you call a host API, you are locked to that environment.

Global Objects: window vs. global

window .document .location .history .navigator .localStorage .alert() / .confirm() Top-level "this" === window global .process .Buffer .setImmediate() .clearImmediate() Top-level "this" === module.exports Both share: console, setTimeout, setInterval, Promise, JSON

DOM vs. File System

The single biggest difference between the two runtimes comes down to what they can touch:

Browser: the DOM
Browsers expose the Document Object Model: a tree of HTML elements you can create, read, update, and delete with JavaScript.
Node.js: the File System
Node.js exposes the fs module: functions to read files, write files, create directories, and interact with the operating system.

Module Systems: require vs. import

JavaScript has two ways to split code into files and load them:

CommonJS (CJS) ES Modules (ESM)
Syntax require() / module.exports import / export
Loading Synchronous Asynchronous
Default in Node.js Browser (<script type="module">)
Cross-compat Not native in browsers Supported in Node.js 14+

How to Decide Which Runtime

1
Identify the task. Does your code need to interact with a webpage (HTML elements, user clicks, styling)?
2
If yes: use the browser. The DOM, CSS animations, and user-event APIs live only in the browser runtime.
3
If the task involves files, servers, or OS access: use Node.js. Reading a CSV, starting an HTTP server, or running a script on your machine requires Node.
4
If it is pure logic: array transformations, string parsing, math calculations: it works in both.
5
Check your APIs. Before using a function, ask: "Is this from the browser or from Node?" A quick search resolves most confusion.

Example: Browser Code

// This only works in a browser
const btn = document.querySelector('#myBtn');

btn.addEventListener('click', () => {
  document.body.style.backgroundColor = '#0f172a';
  window.alert('Theme changed!');
});

Example: Node.js Code

// This only works in Node.js
const fs = require('fs');

fs.readFile('./data.txt', 'utf8', (err, data) => {
  if (err) throw err;
  console.log(data);
});

console.log(process.version); // e.g. "v20.11.0"

Example: Portable Code

// Works in BOTH browser and Node.js
const nums = [3, 1, 4, 1, 5];

const sorted = nums.slice().sort((a, b) => a - b);
const sum = nums.reduce((acc, n) => acc + n, 0);

console.log(sorted); // [1, 1, 3, 4, 5]
console.log(sum); // 14

Rule of thumb: if you only use features listed in the ECMAScript specification (not Web APIs or Node APIs), your code runs everywhere.

Event Loop Differences

Browser Event Loop Call Stack Microtask Queue (Promises) Macrotask Queue (setTimeout) Render Steps (rAF, paint) Rendering is interleaved with tasks Node.js Event Loop Call Stack Microtask Queue (Promises) libuv Phases (6 phases) process.nextTick() No rendering: I/O and timers only

The Security Model

The two runtimes have fundamentally different trust models because they face different threats.

Browser: Sandboxed
Code runs inside a sandbox. It cannot read local files, access other tabs (same-origin policy), or install software. The user is untrusted.
Node.js: Full Access
Code runs with full OS permissions. It can delete files, open network ports, and spawn child processes. The developer is trusted.

Common Mistakes

Using "document" in Node.js
There is no DOM in Node. Calling document.querySelector() in a Node script throws a ReferenceError.
Using "require" in the browser
CommonJS require() is not native to browsers. Use <script type="module"> with ES import/export instead, or a bundler.
Assuming "fs" works everywhere
The file system module is Node-only. Browsers use the File API or fetch() to read user-selected files.

Side-by-Side Comparison

Feature Browser Node.js
Global object window global
DOM access ✓ Yes ✕ No
File system ✕ No ✓ Yes (fs)
Default modules ES Modules CommonJS (ESM opt-in)
Package manager CDN / bundler npm / yarn
Use case UI, interactivity Servers, scripts, CLI
Security Sandboxed Full OS access
Multi-threading Web Workers Worker Threads

Question 1 of 5

Which object is the top-level global in a browser environment?

Question 2 of 5

You need to read a text file from the user's hard drive in a server script. Which runtime and module should you use?

Question 3 of 5

Which of the following code snippets runs identically in both the browser and Node.js?

Question 4 of 5

Why does the browser sandbox JavaScript code?

Question 5 of 5

What does globalThis provide in modern JavaScript?