Chapter I · The Language

How JavaScript Runs

JavaScript code goes through parsing, compilation into bytecode, and execution inside an engine before any instruction takes effect.

01 How JavaScript Runs

Unlike languages that require an explicit compilation step before running on your machine, JavaScript is executed dynamically by a JavaScript engine. However, modern engines do not just interpret text line-by-line; they utilize sophisticated JIT (Just-In-Time) compilation to achieve high performance.

When an engine encounters a script, it first parses the source text into an Abstract Syntax Tree (AST), translates that tree into bytecode, and then begins executing it while simultaneously optimizing hot code paths in the background.

02 Simple Example

A simple loop runs through execution stages inside the engine seamlessly.

run.js
let total = 0;
for (let i = 1; i <= 3; i++) {
  total += i;
}
console.log(total); // 6

Though written as simple text, the engine transforms this loop into optimized executable sequences under the hood.

03 How It Works

The engine handles execution via distinct processing stages: parsing the source code into a syntactic tree, compiling it into bytecode via an interpreter, and optimizing frequently executed sections into native machine code.

Source Code Parser / AST Interpreter Bytecode Machine Code

JavaScript moves from source code to AST, bytecode interpretation, and machine optimization.

04 Step-by-Step

  1. The engine loads the raw script source text.

  2. The parser analyzes the syntax, converting it into an Abstract Syntax Tree (AST).

  3. The interpreter translates the AST into lightweight bytecode and begins execution immediately.

  4. As portions of code run repeatedly ("hot code"), the optimizing compiler compiles them into fast native machine code.

05 Mental Model

"A translator reading a script aloud first, then memorizing fast scenes to perform them at full speed."

Execution starts instantly with interpretation, while frequent tasks are streamlined dynamically behind the scenes for peak efficiency.

06 Common Mistakes

Calling JavaScript "purely interpreted." That description was accurate in the 1990s, but every major engine today (V8, SpiderMonkey, JavaScriptCore) blends interpretation with JIT compilation. Saying "interpreted" alone undersells how the language actually performs.

Best practice: you rarely need to think about parsing or bytecode day-to-day — but understanding that "hot" code gets optimized explains why a function can run slowly the first few times and speed up afterward, which matters when benchmarking code.

07 Interview Questions

Q: What are the main phases a JavaScript engine goes through?

Parsing the source into an AST, compiling to bytecode (and later optimized machine code via a JIT), then executing it.

Q: Is JavaScript compiled or interpreted?

Both, in modern engines — it starts interpreted for fast startup, then hot code paths get compiled to optimized machine code by a JIT compiler.

Key Takeaway

JavaScript runs via an engine that parses text, interprets bytecode instantly, and optimizes frequent routines using Just-In-Time compilation.

Related Concepts