How JavaScript Code Is Parsed and Executed

Beginner · 11 min

What happens between saving a .js file and a line of it running — parsing, bytecode, the JIT, and why "interpreted" undersells what engines like V8 do.

freeCodeCamp Talks · 11 min

How JavaScript Code Is Parsed and Executed

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by freeCodeCamp TalksReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Before anything runs, the engine parses your source text into an AST (abstract syntax tree) — a structured representation the rest of the pipeline can actually work with, not a string of characters.
  • Engines don't just interpret: V8, SpiderMonkey, and JavaScriptCore all start by running bytecode through a fast interpreter, then profile the code as it runs and hand "hot" paths to a JIT compiler that produces real machine code.
  • Calling JavaScript "an interpreted language" is a convenient shorthand, not an accurate one — nothing in the language spec requires an interpreter, and every major engine ships a JIT precisely because pure interpretation is too slow for real programs.
  • An execution context is the environment a piece of code runs in — which variables it can see and what `this` refers to — and the engine creates a new one every time a function is called.
  • Optimizations aren't permanent. If the engine's assumptions about a "hot" piece of code turn out to be wrong (a variable's type changes, say), it deoptimizes back to the slower, safer path rather than keep running incorrect fast code.

The question this lesson answers

You write const total = price * quantity;, save the file, and a number shows up on screen a moment later. Nothing about that sentence explains how — and if you’re going to spend a career writing JavaScript, it’s worth knowing what’s actually happening underneath, even at a conceptual level. Not because you’ll ever need to patch V8 yourself, but because things like “why is this loop suddenly fast after it’s run a few times” or “why did changing one line slow everything down” only make sense once you know code doesn’t run exactly as written — it gets transformed first.

From text to tree

The first thing any JavaScript engine does with your file is read it as plain text and break it into meaningful chunks — numbers, identifiers, keywords, punctuation. That alone isn’t enough to run anything; price, *, and quantity sitting next to each other need to be understood as multiplication, not just three unrelated tokens. The engine assembles those tokens into an AST — an abstract syntax tree — where each node represents an actual operation: “multiply these two things,” “assign this result to that name.” Everything downstream — interpretation, compilation, optimization — works on this tree, not on your original text.

Interpreter first, compiler for what’s worth it

Here’s the part that breaks the “JavaScript is interpreted” story: modern engines don’t pick one strategy and stick with it. They start by turning the AST into bytecode and running it through a fast interpreter, because that gets your program executing almost instantly — nobody wants to wait seconds for a compiler before a web page does anything.

But interpreting bytecode step by step is slow compared to native machine code. So the engine watches itself run: if a function gets called thousands of times, or a loop spins long enough, that code is “hot,” and a JIT (just-in-time) compiler steps in to translate it into real machine code tailored to how it’s actually being used. That’s the video’s core story — watching that pipeline assembled piece by piece, visually, is a better way to absorb it than reading a diagram, which is exactly why it’s the embed for this lesson.

The tradeoff cuts both ways. Compiling takes time and the optimized code makes assumptions — about argument types, say — based on what it’s actually seen run. If a later call breaks that assumption, the engine deoptimizes: it throws away the fast path and falls back to the safer, slower bytecode rather than execute code built on a now-false premise. That’s not a bug; it’s the system working as designed, trading some predictability for speed on the common case.

Why “interpreted” undersells it

Calling JavaScript “interpreted” (usually in contrast to a “compiled” language like C) isn’t wrong so much as incomplete. The language specification itself never mandates how an engine has to run code — it only defines what the result has to be. V8 in Chrome and Node, SpiderMonkey in Firefox, and JavaScriptCore in Safari are all free to interpret, compile, or do both, and in practice every one of them does both, because pure interpretation is too slow for anything real and pure upfront compilation would make every page load feel sluggish while it compiles code that might only run once.

Execution contexts: where code actually lives while it runs

Every time code runs — a script starting, a function being called — the engine sets up an execution context: a record of what variables exist, where to find them, and what this refers to in that spot. The global context exists for as long as your program does; a function gets a brand new one on every single call, which is also where its local variables and parameters live until the call returns. This is the mechanism that makes scope, closures, and this behave the way they do — all topics this roadmap gets to shortly, and all of them easier to reason about once you know there’s a concrete, created-and-discarded object behind the scenes rather than some abstract rule.

Where this fits

This is the opening lesson for a reason: almost everything else in this roadmap assumes you have some mental model of “code runs inside something, in some order, and that something gets built before execution starts.” Variables, scope, and hoisting leans directly on the execution-context idea introduced here, and the asynchronous-JavaScript group later on assumes you already know code isn’t executed in one flat, uninterrupted pass. You don’t need to memorize engine internals to use JavaScript well — but knowing this much removes a lot of mystery from behavior that otherwise looks arbitrary.

Check yourself

  1. 01Why can't a JavaScript engine just run your source code directly, character by character, as it reads the file?

    Source code is just text — the engine first has to figure out its structure (which characters form a variable name, which form an operator, how statements nest) before it means anything executable. That structuring step produces an AST, a tree the engine can actually walk and act on, which is a different job from reading characters in order.

  2. 02What's inaccurate about calling JavaScript "an interpreted language"?

    That label suggests code is only ever read and executed line by line with no compilation step. In practice, every major engine compiles: it starts with a quick interpreter for immediate results, then compiles frequently-run ("hot") code into optimized machine code with a JIT compiler. JavaScript is both interpreted and compiled, depending on which part of a program you're looking at and when.

  3. 03What is an execution context, and when does the engine create a new one?

    It's the environment a chunk of code runs in — the variables and functions it can reach, and what `this` means inside it. The engine creates one for the global scope when a script starts, and a new one every single time a function gets called, which is also where that function's local variables live.

  4. 04Why would an engine ever "deoptimize" code it already compiled to fast machine code?

    JIT compilers optimize based on assumptions learned by watching the code run — for example, assuming a function's argument is always a number because that's all it's seen so far. If a later call breaks that assumption (passing a string instead), the optimized machine code is no longer valid for that case, so the engine falls back to the slower, more general bytecode path rather than produce a wrong result.

  5. 05What's the practical difference between the interpreter and the JIT compiler inside an engine like V8?

    The interpreter (V8 calls its Ignition) turns the AST into bytecode and starts running it almost immediately, favoring fast startup over raw speed. The JIT compiler (V8's TurboFan) watches which bytecode runs often and recompiles just that code into native machine code, trading a bit of upfront compile time for much faster execution on the paths that matter.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark