Skip to content
Getting StartedChapter 2 / 47Beginner5 min read

How JavaScript Runs

Learn what a JavaScript engine and runtime are, how V8 and the call stack work, and how code runs in the browser and Node.js. A simple mental model.

On this page

1. What is a JavaScript runtime?

The JavaScript text you write does nothing by itself. You need an engine to run it. The engine alone only understands the language. Things like page buttons, fetch and setTimeout are provided by the runtime.

RuntimeEngineExtra features
Chrome / EdgeV8DOM, fetch, localStorage, timers
FirefoxSpiderMonkeyDOM, fetch, timers
SafariJavaScriptCoreDOM, fetch, timers
Node.jsV8File system, process, fetch, timers
Deno / BunV8 / JavaScriptCoreFile system, web APIs, built-in tools

2. Why does it matter?

Beginners often assume document is available everywhere. Then Node.js shows a document is not defined error. Understanding the difference between engine and runtime clears up this confusion.

Not knowing the differenceKnowing the difference
Trying to use document everywhereKnowing that only the browser provides document
Error reasons make no senseSpotting a runtime problem from the error
Code order feels confusingThe call stack makes the order clear
Treating setTimeout as magicKnowing the runtime runs the timer, not the engine

3. Where is it used?

SituationHow this knowledge helps
Frontend codeYou know the DOM and window come from the browser runtime
Backend codeNode.js gives you fs and process, not the DOM
DebuggingThe call stack shows up in an error's stack trace
PerformanceYou understand why a heavy loop blocks the main thread
Async codeThe foundation for understanding the Event Loop

4. How does it work?

The engine first parses the code (turns text into a structure). Then modern engines JIT (Just In Time) compile the code: first a version that starts quickly, and then more optimization for code that runs again and again.

Inside the engine
JavaScript source code
Parser (turns code into a structure)
Interpreter (starts running code quickly)
JIT compiler (makes hot code fast)
Machine code runs

JavaScript is single threaded. This means only one thing runs at a time. Every function call goes onto the call stack, and leaves the stack when its work is done.

Simple call stack flow
Global code on the stack
greet() is called, pushed on the stack
greet() finishes, popped off the stack
The next line runs

Slow work like timers and network requests is handled by the runtime itself. When that work is ready, its callback is placed in a queue and runs once the stack is empty. This whole system is called the event loop.

5. Code example

See the order of the call stack:

js
function greet(name) {
  console.log(`Hello, ${name}`);
}

console.log("Start");
greet("Ali");
console.log("End");

Output: Start, then Hello, Ali, then End. Each line runs in its turn.

Now see the runtime at work:

js
console.log("1");

setTimeout(() => {
  console.log("2 (from the timer)");
}, 0);

console.log("3");

Output: 1, 3, then 2 (from the timer). The timer is 0 ms, yet the callback runs later, because it comes out of the queue only when the call stack is empty.

How does the code work?

ItemMeaning
greet("Ali")The function goes onto the call stack and leaves when it finishes
setTimeoutA feature of the runtime, not of the engine
0 ms delayThe minimum wait, not a guarantee of running immediately
Output orderAll sync code first, then the callbacks from the queue

Interview tip

"JavaScript is single threaded, so how does it do async work?" Answer: the engine is single threaded, but the runtime (the browser or Node.js) handles timers and network in the background, and the event loop runs their callbacks when the call stack is empty.

6. Common mistakes

Mistake: Using document or window in Node.js

Why? These are objects of the browser runtime. They do not exist in Node.js, so you get a ReferenceError.

Better: First check where the code will run. If you need code that works in both places, use globalThis.

Mistake: Thinking setTimeout(fn, 0) runs immediately

Why? The callback goes into the queue and runs only after all the sync code has finished.

Better: Think about the order in terms of the call stack and the event loop, and treat the delay as only a minimum time.

Mistake: Running a very heavy loop on the main thread

Why? The single thread gets blocked, the page freezes in the browser and clicks stop working.

Better: Break heavy work into small pieces or move it to a Web Worker.

7. What to learn before and after

Final quick revision

PointDetail
EngineParses, compiles and runs code (V8, SpiderMonkey)
RuntimeEngine + extra APIs (browser or Node.js)
JITOptimizing code at runtime when it runs again and again
Single threadedOne task at a time, one call stack
Async workThe runtime handles it, the event loop runs the callback