JavaScript Under the Hood: Engine, Runtime, and the Execution Context Lifecycle
What you will learn
By the end of this chapter, you should be able to explain:
What a JavaScript Engine does.
What a JavaScript Runtime is and why we need it.
How JavaScript creates and executes an Execution Context.
How the Call Stack manages function execution.
1. JavaScript Engine: The Place Where JavaScript Gets Executed
Imagine you write a JavaScript program:
const name = "Alex";
console.log("Hello " + name);
Your computer does not directly understand JavaScript. The processor understands machine instructions.
So, how does JavaScript code actually run?
This is where the JavaScript Engine comes in.
A JavaScript Engine is a program that reads, processes, and executes JavaScript code.
Some popular JavaScript engines are:
Engine | Used in |
|---|---|
V8 | Google Chrome, Node.js, and other environments |
SpiderMonkey | Firefox |
JavaScriptCore | Safari |
For example, Chrome and Node.js both use Google's V8 engine.

1.1 How Does a JavaScript Engine Work?
A JavaScript engine does not simply take your code and execute it immediately. It processes the code through several steps.
Let's understand the main steps using V8 as an example.
Step 1: Parsing
The engine first reads your source code and breaks it into smaller pieces called tokens.
It then checks how those pieces are arranged and builds a structure called an Abstract Syntax Tree (AST).
Think of the AST as a structured representation of your code that the engine can understand.
Step 2: Execution
V8 uses an interpreter called Ignition to generate and execute bytecode.
Bytecode is a lower-level set of instructions that is easier for the engine to work with than the original JavaScript source code.
Step 3: Optimization
When the engine notices that certain code runs frequently, it may optimize that code using compiler technologies such as TurboFan.
The optimized code can run more efficiently.
V8 also uses additional execution tiers, so not every piece of code follows exactly the same path.

Remember: You do not need to memorize every internal detail of V8 right now. The important idea is that the JavaScript engine processes your code and makes it executable by the computer.
2. JavaScript Runtime: Everything Around the Engine
Now we understand what a JavaScript engine does. But there is another question:
Is the JavaScript engine alone enough to run a complete application?
Not always.
JavaScript applications often need to interact with their environment.
For example:
A browser application needs access to the DOM to update a webpage.
A Node.js application may need to read files from the computer.
Applications need access to other environment-provided features.
The JavaScript engine does not provide all these features by itself.
This is where the JavaScript Runtime comes in.
A JavaScript Runtime is the environment that provides the JavaScript engine along with additional features needed to run JavaScript applications.
For example:
Browser Runtime | Node.js Runtime |
|---|---|
JavaScript Engine | JavaScript Engine |
DOM APIs | File System APIs |
Browser APIs | Node.js APIs |
Browser environment | Node.js environment |
Both environments can use the V8 engine, but they provide different features around it.

The key difference:
JavaScript Engine: Processes and executes JavaScript code.
JavaScript Runtime: Provides the engine and the surrounding environment in which JavaScript applications run.
Now that we understand where JavaScript runs, let's see what happens when the engine starts executing our code.
3. Execution Context: Where JavaScript Code Runs
Every time JavaScript executes code, it uses an Execution Context.
Think of an Execution Context as an internal environment that keeps track of the information needed while a particular piece of code is running.
An Execution Context has two important components:
1. Memory Creation Phase
The Memory Component stores information about variables and functions.
It stores this information in key-value form.
It is also commonly called the Variable Environment.
2. Code Execution Phase
The Code Component is responsible for executing the code one instruction at a time.
It is also called the Thread of Execution.

JavaScript executes code synchronously on its execution thread: it processes one instruction at a time in order.
In this chapter, we are focusing on normal synchronous execution. We will cover asynchronous JavaScript in a later chapter.
4. How JavaScript Executes Code
When a JavaScript program starts, JavaScript creates a Global Execution Context (GEC).
The Global Execution Context is the first execution context created for the program's top-level code.
It is created in two phases:
Memory Creation Phase
Code Execution Phase
Let's understand both phases with an example.
var n = 2;
function square(num) {
var ans = num * num;
return ans;
}
var square2 = square(n);
var square4 = square(4);
4.1 Phase One: Memory Creation
Before executing the code line by line, JavaScript prepares memory for the declarations in the current execution context.
For this example:
Name | Initial value |
|---|---|
|
|
| Function definition |
|
|
|
|
Notice two things:
Variables declared using
varare initially assignedundefined.The function declaration is stored with its function definition.
This is the Memory Creation Phase.

4.2 Phase Two: Code Execution
After preparing memory, JavaScript starts executing the code line by line.
First line:
var n = 2;
The value of n changes from undefined to 2.
Next:
function square(num) {
var ans = num * num;
return ans;
}
JavaScript already stored the function definition during the Memory Creation Phase. It does not execute the function body until the function is called.
Next:
var square2 = square(n);
Here, JavaScript encounters a function call.
Something important happens: JavaScript creates a new Function Execution Context for square().
Let's understand that next.
5. Function Execution Context: What Happens When a Function Is Called?
Every time a function is called, JavaScript creates a new Function Execution Context for that call.
Consider:
var square2 = square(n);
JavaScript creates a new execution context for the square() function.
Just like the Global Execution Context, this function context also has two phases.
Phase One: Memory Creation
JavaScript prepares memory for the function's local variables and parameters.
For this call:
square(2);
The memory initially looks like this:
Name | Initial value |
|---|---|
|
|
|
|
Phase Two: Code Execution
Now JavaScript executes the function body:
var ans = num * num;
return ans;
The argument
2is assigned tonum.JavaScript calculates
2 * 2.The result
4is assigned toans.return anssends the result back to the place where the function was called.
After the function returns, its Function Execution Context is removed from the Call Stack.
The returned value 4 is assigned to square2 in the Global Execution Context.
The same process happens when JavaScript executes:
var square4 = square(4);
This time, the function returns 16.

6. Call Stack: How JavaScript Tracks Function Execution
We have now seen that JavaScript creates a new Execution Context whenever a function is called.
But how does JavaScript keep track of all these execution contexts?
It uses a mechanism called the Call Stack.
The Call Stack keeps track of the active execution contexts and the order in which functions need to finish.
It follows LIFO (Last In, First Out).
This means the last function added to the stack is the first one to finish and be removed.
Let's understand the flow:
When the program starts, the Global Execution Context is pushed onto the Call Stack.
When
square(n)is called, its Function Execution Context is pushed on top.When the function returns, its context is popped off the stack.
JavaScript continues executing the Global Execution Context.
The same process happens for
square(4).

The Call Stack is also known by other names:
Execution Context Stack
Program Stack
Runtime Stack
Important distinction:
An Execution Context contains the information needed to execute a particular piece of code.
The Call Stack keeps track of active execution contexts.
7. Complete Code Walkthrough
Let's bring everything together.
var n = 2;
function square(num) {
var ans = num * num;
return ans;
}
var square2 = square(n);
var square4 = square(4);
Step | What happens |
|---|---|
1 | JavaScript creates the Global Execution Context. |
2 | Memory is prepared for |
3 | JavaScript starts executing the global code. |
4 |
|
5 |
|
6 | The function calculates and returns |
7 |
|
8 |
|
9 |
|
Final values:
n // 2
square2 // 4
square4 // 16
When the program has finished executing, the function contexts have already been removed. The Global Execution Context remains associated with the running script or environment; it should not be assumed that every global context is immediately destroyed after the last line.
8. Quick Revision
Concept | Remember this |
|---|---|
JavaScript Engine | Processes and executes JavaScript code. |
JavaScript Runtime | Provides the engine and environment features. |
Execution Context | The environment used while a piece of JavaScript code runs. |
Memory Component | Stores information about variables and functions. |
Code Component | Executes instructions one at a time. |
Global Execution Context | Created when JavaScript starts executing a script. |
Function Execution Context | Created whenever a function is called. |
Memory Creation Phase | Prepares memory for declarations. |
Code Execution Phase | Executes the code and assigns values. |
Call Stack | Tracks active execution contexts using LIFO order. |