Execution context in JavaScript
What is an Execution Context?
An execution context is the environment in which JavaScript code is evaluated and executed. It contains all the information needed to run the code: variables, this, scope chain, and more.
Types of Execution Context
| Type | Description |
|---|---|
| Global Execution Context | Created when the script starts. Only one exists. Creates the global object (window/globalThis) and sets this to it. |
| Function Execution Context | Created each time a function is invoked. Each function gets its own context. |
| Eval Execution Context | Created inside an eval() call (rarely used). |
Execution Context Phases
Every execution context goes through two phases:
1. Creation Phase
- Creates the Lexical Environment (stores
let/const, functions) - Creates the Variable Environment (stores
vardeclarations) - Sets up
thisbinding - Creates the scope chain (reference to outer environment)
- Variables declared with
varare initialized toundefined(hoisting) - Variables declared with
let/constare in the Temporal Dead Zone - Function declarations are fully hoisted (available before their line)
2. Execution Phase
- Code is executed line by line
- Variables are assigned their values
- Functions are called, creating new execution contexts
The Call Stack
The call stack (execution context stack) is a LIFO (Last In, First Out) structure that tracks the currently executing context:
function first() {
console.log("first");
second();
console.log("first end");
}
function second() {
console.log("second");
third();
}
function third() {
console.log("third");
}
first();Call stack progression:
1. [Global]
2. [Global] β [first()]
3. [Global] β [first()] β [second()]
4. [Global] β [first()] β [second()] β [third()]
5. [Global] β [first()] β [second()] // third() pops
6. [Global] β [first()] // second() pops
7. [Global] // first() popsWhat's Inside an Execution Context
ExecutionContext = {
LexicalEnvironment: {
EnvironmentRecord: {
// let, const, function declarations
},
outer: <reference to parent Lexical Environment>
},
VariableEnvironment: {
EnvironmentRecord: {
// var declarations
},
outer: <reference to parent>
},
ThisBinding: <value of this>
}Practical Example
var x = 10;
let y = 20;
function add(a, b) {
var result = a + b;
return result;
}
const sum = add(x, y);Creation phase (Global):
x= undefined (var hoisted)y= TDZ (let β temporal dead zone)add= function (fully hoisted)sum= TDZ (const)
Execution phase (Global):
x= 10y= 20add(10, 20)is called β new Function Execution Context
Creation phase (add):
a= 10,b= 20 (parameters)result= undefined (var hoisted)
Execution phase (add):
result= 30- Returns 30
Stack Overflow
// Infinite recursion β stack overflow
function infinite() {
infinite();
}
infinite(); // RangeError: Maximum call stack size exceededImportant:
Understanding execution context explains hoisting, scope, closures, and this binding. Every function call creates a new context on the call stack. The engine uses creation and execution phases to set up variables before running code.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.