All 28 lessons ▾

Inside JavaScript · Lesson 16 of 28

Inside the Engine: How V8 Runs Your JavaScript

Updated 11 Nov 2026 · By Akshay, software engineer, India · 8 min read

So your JavaScript isn't run by magic. Inside the engine, it's split into tokens, turned into a tree, and compiled while it runs. So let's open it up.

The video for this lesson is coming soon.

A new lesson goes up every day at 11 am. Subscribe on YouTube so you don't miss it.

Got a doubt? Ask in the comments under the video.Subscribe

In one minute
  • Runtime = engine + APIs + event loop + queues
  • Engines follow ECMAScript: V8, SpiderMonkey, JavaScriptCore
  • Parsing: tokens, then an AST
  • JIT: Ignition interprets, TurboFan optimizes
  • Execution: memory heap + call stack
  • Orinoco collects garbage with mark and sweep

Part 1JavaScript runs everywhere

So far we've seen what happens to our code. So today, we go inside the machine that runs it. The JavaScript engine. Now let's start with a fun fact. JavaScript runs almost everywhere today. Browsers, servers, smart watches, even robots and light bulbs. So how is that possible? Because of something called the JavaScript runtime environment.

JavaScript runtime environment
JS engine
the heart
APIs
talk to the outside
Event
loop
Callback
queue
Microtask
queue
REMEMBER THIS
Runtimeenvironment=
engine+APIs+event
loop+queues.
the browser and Node.js are both
runtimes

So a JavaScript runtime environment is like a big container, with everything needed to run JavaScript code. At its heart is the JavaScript engine. Around it, there are APIs to talk to the outside world, like the Web APIs we saw last time. And the event loop, with the callback queue, and the microtask queue. Now a browser can run your code because it has a runtime environment inside it. And Node.js is another runtime, with the same engine, V8, but different APIs. In the browser, you get the DOM and local storage. In Node, you get files and servers instead. Some APIs, like setTimeout, exist in both, but they're built differently inside.

So remember this. The engine is the heart, but it's not the whole thing. Engine, plus APIs, plus the event loop and its queues, together make a runtime environment. The browser is one runtime. Node.js is another. So that's why the same JavaScript language can run in so many different places.

Part 2Many engines, one rulebook

Now, there isn't just one JavaScript engine. There are many, made by different companies. Let's see them.

ECMAScript
the rulebook
V8
Chrome and Node.js
SpiderMonkey
Firefox: the first
engine
JavaScriptCore
Safari
REMEMBER THIS
Anengineisnota
machine.It'saprogram.
V8 is written in C++, and turns
JavaScript into machine code

So every engine follows the same rulebook. It's called ECMAScript. It's the standard that says exactly how JavaScript must behave. Google's engine is V8. It runs Chrome, Node.js, and Deno too. Firefox has SpiderMonkey, which was the very first JavaScript engine, made by Brendan Eich, the person who created JavaScript itself. And Safari has JavaScriptCore. Different companies, different engines, all competing to be the fastest. But they all follow ECMAScript, so your code behaves the same everywhere.

And now here's something people often imagine wrong. A JavaScript engine is not a machine, or some special hardware. It's just a program. V8, for example, is written in C plus plus. It takes your high-level JavaScript, and turns it into low-level machine code, which your computer's processor can actually run. So that's its whole job. Now let's see how it does it.

Part 3Step one: parsing

So inside the engine, your code goes through three big steps. Parsing, compilation, and execution. Let's take them one by one, starting with parsing.

tokens.js
1let a = 7;2 3// tokens:  let   a   =   7

So first, the engine reads your code, and breaks it into small pieces, called tokens. Now look at this line. let a equals seven. It becomes four tokens. let, a, the equals sign, and seven. Just like you split a sentence into words before you understand it. Then a syntax parser takes these tokens, and checks that they follow the grammar of JavaScript. If something's wrong, like a missing bracket, this is where you get a syntax error, before a single line runs. Remember those from episode eight? So now you know where they come from.

ast.js
1// let a = 7;  as a tree (AST)2{3  type: "VariableDeclaration",4  kind: "let",5  declarations: [{6    id:   { type: "Identifier", name: "a" },7    init: { type: "Literal", value: 7 }8  }]9}

And now the parser builds something called an abstract syntax tree. AST, for short. It's your code, rewritten as a tree of objects that the engine can easily work with. So here's our line as a tree. A variable declaration, of kind let. It declares an identifier named a, and starts it with the literal value seven. Now want to see this for any code? Go to a website called AST explorer, paste your code, and you'll see its tree instantly. Tools like Babel and ESLint work with these trees too.

Part 4Step two: compiling, just in time

Now comes the most interesting question of today. Is JavaScript an interpreted language, or a compiled one? So let's settle it. Think about it first.

Interpreter
runs line by line, right away
Compiler
translates everything first
starts fast
runs fast

So first, the two classic ways to run code. An interpreter reads your code, and runs it line by line, right away. It starts fast, but it doesn't optimize much. Now a compiler first translates the whole program into optimized machine code, and then runs it. It's slower to start, but the result runs very fast. So two trade-offs. Speed of starting, versus speed of running.

Your code
AST
Ignition
interpreter: bytecode
Execution
TurboFan
compiler: machine code
bytecode
hot code
fast code
deoptimize

And JavaScript? Modern engines use both, together. It's called just-in-time compilation, or JIT. So here's how V8 does it. Your code becomes an AST. Then V8's interpreter, called Ignition, turns the AST into bytecode, and starts running it right away. So your page starts quickly. While it runs, V8 watches which functions are used again and again. That's called hot code. Hot code is sent to the optimizing compiler, called TurboFan, which turns it into very fast machine code. And if TurboFan's guesses turn out wrong, say a function that always got numbers suddenly gets a string, the code is deoptimized, and goes back to bytecode. The best of both worlds. Along the way, TurboFan uses clever tricks, with names like inlining, inline caching, and copy elision. Some engines even compile parts of the code ahead of time. Every company competes to make its engine faster, and today V8 is known as one of the fastest.

REMEMBER THIS
JavaScriptisJITcompiled:
interpretedtostartfast,compiled
whereit'shot.
Ignition interprets, TurboFan optimizes

So remember this. Is JavaScript interpreted or compiled? It depends on the engine, but modern engines do both. That's just-in-time compilation. The interpreter gets your code running fast, and the compiler makes the busy parts even faster, while the program is running. So yes, JavaScript does get compiled. Now a lot of people get this wrong in interviews.

Part 5Step three: execution

And finally, the code runs. So for that, the engine needs two things you already know, and one helper you don't. Let's see.

Memory heap
objects and functions live
here
Call stack
code runs here
Garbage collector
Orinoco, in V8
reads data
frees memory

So first, the memory heap. It's a big area of memory, where objects, arrays, and functions are stored. Then, the call stack, our old friend, where execution contexts are pushed and popped. They work together. The call stack runs the code, and reaches into the heap for data. And the helper is the garbage collector. In V8, it's called Orinoco. It finds memory that nothing points to anymore, and frees it, so your program doesn't fill up the memory.

mark-and-sweep.js
1let user = { name: "Asha" };2let temp = { size: 99 };3 4temp = null;5 6// mark:  everything reachable from global7// sweep: { size: 99 } is cleared away

So it uses an algorithm called mark and sweep. Here's the idea. Starting from the global object, it follows every reference, and marks everything it can reach. user's object is reachable, so it's marked. But when temp becomes null, nothing points to that size object anymore. It can't be reached, so it isn't marked. Then comes the sweep. Everything not marked is cleared away. So that's how memory is freed automatically, and it's why closures, which keep references, keep things alive.

INTERVIEW ANSWER
Parseintotokensandan
AST,interprettobytecode,
optimizehotcode,execute
withheapandstack.
in V8: Ignition, TurboFan, Orinoco
Quick quiz
quiz.js
1function add(a, b) {2  return a + b;3}4 5for (let i = 0; i < 100000; i++) {6  add(i, 1);7}8 9add("Hi ", "there");

Add is called a hundred thousand times, always with numbers. What will V8 do with it?

Show the answer

So it becomes hot code, so TurboFan compiles it into fast machine code, specialized for numbers. And the last line? It suddenly passes strings! The optimized code was built for numbers, so V8 deoptimizes add, back to bytecode, and you still get the right answer. So the lesson? Keep your functions consistent with their types, and the engine can keep them fast.

Your turn

Did you think JavaScript was only interpreted?

Answer in the comments under the video.

Your progress stays on this device.
A new lesson every day at 11 am

The video on YouTube, and the lesson as a swipe-through on Instagram.