How JavaScript Runs
A JavaScript engine is a program that reads JavaScript source code and runs it. A runtime is the engine plus everything around it that a program can use, such as the page in a browser or the file system in Node.js. Knowing the difference explains why document works in Chrome but not in Node.js, why a long loop freezes a page, and why the same code can run slowly at first and fast a moment later.
In One Minute
- The engine runs the language: V8 (Chrome, Edge, Node.js, Deno), SpiderMonkey (Firefox) and JavaScriptCore (Safari, Bun).
- The runtime adds the tools: the browser adds
document,fetchand timers; Node.js adds files, processes and networking. - JavaScript runs your code on one main thread: one thing at a time. Slow work is handed off, and its result comes back later as an event.
- Engines don't simply interpret line by line. They parse the code, run it as bytecode, then compile hot code to fast machine code while the program runs (just-in-time compilation).
Engine and Runtime
An everyday comparison: the engine is a car's motor, and the runtime is the whole car. The same motor can sit in a sedan or a truck. In the same way, the V8 engine sits inside Chrome and inside Node.js, but the two give it very different tools.
| Runtime | Engine | What it adds |
|---|---|---|
| Chrome, Edge | V8 | The DOM (document), window, fetch, timers, storage, events |
| Firefox | SpiderMonkey | The same Web APIs |
| Safari | JavaScriptCore | The same Web APIs |
| Node.js 26 | V8 | process, files (node:fs), networking (node:http), fetch, timers |
| Deno | V8 | Web APIs plus a permission system |
| Bun | JavaScriptCore | Node.js-compatible APIs, a fast package manager |
The language itself (variables, functions, arrays, Math, JSON, promises) is the same everywhere. That shared part is ECMAScript. The program below uses only the language, so it prints the same thing in every runtime:
const fees = [120, 80, 45];
const total = fees.reduce((sum, fee) => sum + fee, 0);
console.log("Total:", total);
console.log(typeof globalThis);Total: 245
objectExplanation: fees.reduce adds the three numbers. globalThis is the name every runtime gives its global object: window in a browser page, global in Node.js. Writing globalThis works in both.
One Thread, One Thing at a Time
JavaScript runs your code on a single main thread. Imagine a cashier with one register: customers are served one at a time, in order. If one customer takes ten minutes, everyone behind waits.
So how does a page download data, run a timer and respond to clicks "at once"? The slow parts are not done by JavaScript itself. The runtime does the waiting (the network, the clock) in the background, and when the result is ready, it puts a small task in a queue. The main thread picks up queued tasks only when it has finished what it is doing.
console.log("1. Start");
setTimeout(() => console.log("3. Timer finished"), 0);
console.log("2. End of the script");1. Start
2. End of the script
3. Timer finishedExplanation: setTimeout asks the runtime to run a function after 0 milliseconds. The runtime queues it, but the main thread is still busy running the script, so the timer's function runs only after line 3. This queue-and-wait system is the event loop, explained fully in the lesson on it later in the course.
Common Mistakes
Using browser features in Node.js (or the reverse). document.querySelector fails in Node.js with ReferenceError: document is not defined, and require("fs") doesn't exist in a browser page. Know which runtime your code is for.
Believing JavaScript can run two of your functions at the same time. On the main thread it can't. Code that "waits" (a timer, a network request) gives the thread back and continues later.
Thinking JavaScript is slow because it is "interpreted". Hot code is compiled to machine code. Most slowness on web pages comes from doing too much work on the page (layout and rendering), not from the language.
Under the Hood
Modern engines turn your source code into machine code in several steps. V8 is the best documented, so here is its pipeline as of Node.js 26 and Chrome in 2026:
- Parsing. The source text is split into tokens (
const,fees,=, ...) and built into an abstract syntax tree (AST), a tree that represents the structure of the program. Syntax errors are found here, before any line runs. To start fast, functions that aren't called right away are only pre-parsed. - Bytecode. The Ignition interpreter turns the AST into compact bytecode and starts running it immediately. While it runs, it records type feedback: which kinds of values each operation really sees.
- Optimizing compilers. When a function runs often ("hot" code), V8 compiles it to machine code. Sparkplug makes quick, lightly optimized code; Maglev is a fast optimizing compiler; TurboFan produces the most optimized code for the hottest functions, using the type feedback to make assumptions ("this
+always adds two numbers"). - Deoptimization. If an assumption breaks (the
+suddenly gets a string), V8 throws away the optimized code and goes back to bytecode, then may optimize again later.
This is why JavaScript is described as just-in-time (JIT) compiled, not simply interpreted. It also explains a practical rule: functions that always receive the same kinds of values stay fast, and functions that receive a mix are harder to optimize. You will meet this again in the performance lesson.
You can see the parse step on its own: a syntax error stops the whole script before its first line runs.
console.log("This line never runs");
const total = ;SyntaxError: Unexpected token ';'Explanation: the engine parses the entire script before running it, finds const total = ;, and reports a SyntaxError. Because parsing failed, not even the first console.log ran.
Interview Corner
What is the difference between a JavaScript engine and a runtime?
The engine (V8, SpiderMonkey, JavaScriptCore) parses and executes the ECMAScript language: values, functions, objects, promises. The runtime embeds the engine and adds APIs and an event loop: browsers add the DOM, fetch and timers; Node.js adds files, processes and networking. setTimeout and document are runtime features, not language features.
JavaScript is single-threaded. How can it handle many things at once?
Your code runs on one main thread, but waiting is done by the runtime: network requests, timers and file reads proceed in the background. When one finishes, the runtime queues a callback, and the event loop runs it when the call stack is empty. That gives concurrency without parallel execution of your code. True parallelism needs Web Workers or Node.js worker threads.
Is JavaScript interpreted or compiled?
Both. V8 parses source to an AST, runs it as bytecode in the Ignition interpreter while collecting type feedback, and compiles hot functions to machine code with Sparkplug, Maglev and TurboFan. If the optimized code's assumptions fail, it deoptimizes back to bytecode.
What does this print?
Show solution
setTimeout(() => console.log("A"), 0);
console.log("B");
setTimeout(() => console.log("C"), 0);
console.log("D");B
D
A
CThe two console.log calls in the script run first. The timers run afterward, in the order they were scheduled.
Practice
Challenge: Predict the output of the program below, then press Try it to check. Then change it so that "Ready" prints last without moving the line.
console.log("Loading");
console.log("Ready");
console.log("Done");Show solution
console.log("Loading");
setTimeout(() => console.log("Ready"), 0);
console.log("Done");Loading
Done
ReadyKey Takeaways
| Term | Meaning |
|---|---|
| Engine | Runs JavaScript code (V8, SpiderMonkey, JavaScriptCore) |
| Runtime | Engine plus APIs and an event loop (browser, Node.js, Deno, Bun) |
| Main thread | The single thread that runs your code |
| AST | The tree the parser builds from source code |
| JIT compilation | Compiling hot code to machine code while the program runs |
globalThis | The global object in every runtime |
Was this tutorial helpful?