How JavaScript Runs

Updated · 8 min read
easyES2026javascriptenginev8runtimejitinterview

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, fetch and 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.

RuntimeEngineWhat it adds
Chrome, EdgeV8The DOM (document), window, fetch, timers, storage, events
FirefoxSpiderMonkeyThe same Web APIs
SafariJavaScriptCoreThe same Web APIs
Node.js 26V8process, files (node:fs), networking (node:http), fetch, timers
DenoV8Web APIs plus a permission system
BunJavaScriptCoreNode.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:

same-everywhere.js▶ Try it
const fees = [120, 80, 45];
const total = fees.reduce((sum, fee) => sum + fee, 0);
console.log("Total:", total);
console.log(typeof globalThis);
Output
Total: 245
object

Explanation: 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.

order.js▶ Try it
console.log("1. Start");
setTimeout(() => console.log("3. Timer finished"), 0);
console.log("2. End of the script");
Output
1. Start
2. End of the script
3. Timer finished

Explanation: 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:

  1. 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.
  2. 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.
  3. 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").
  4. 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.

parse-first.js▶ Try it
console.log("This line never runs");
const total = ;
Output
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
puzzle.js▶ Try it
setTimeout(() => console.log("A"), 0);
console.log("B");
setTimeout(() => console.log("C"), 0);
console.log("D");
Output
B
D
A
C

The 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.

practice.js
console.log("Loading");
console.log("Ready");
console.log("Done");
Show solution
practice-solution.js▶ Try it
console.log("Loading");
setTimeout(() => console.log("Ready"), 0);
console.log("Done");
Output
Loading
Done
Ready

Quick check: How JavaScript runs

3 questions
1Which engine does Node.js use?
2Where does document come from?
3Why does a syntax error stop even the first line of a script?

Key Takeaways

TermMeaning
EngineRuns JavaScript code (V8, SpiderMonkey, JavaScriptCore)
RuntimeEngine plus APIs and an event loop (browser, Node.js, Deno, Bun)
Main threadThe single thread that runs your code
ASTThe tree the parser builds from source code
JIT compilationCompiling hot code to machine code while the program runs
globalThisThe global object in every runtime

Was this tutorial helpful?

Lesson 2 of 139