Browser vs. Node.js
Discover how the same language runs in two very different worlds: the browser tab and the server terminal.
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.
Two Runtimes, One Engine
The Core Rule
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
DOM vs. File System
The single biggest difference between the two runtimes comes down to what they can touch:
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
Example: Browser Code
const btn = document.querySelector('#myBtn');
btn.addEventListener('click', () => {
document.body.style.backgroundColor = '#0f172a';
window.alert('Theme changed!');
});
Example: Node.js Code
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
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
The Security Model
The two runtimes have fundamentally different trust models because they face different threats.
Common Mistakes
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?