Master the 2-phase execution model: Memory Creation Phase and Code Execution Phase managed inside the Call Stack container.
Learn how variables act as labeled containers for data using const, let, and var to manage lexical scope and reassignment safely in modern applications.
In JavaScript, everything happens inside an Execution Context. Think of an Execution Context as a sealed container where JavaScript evaluates and executes your code.
Understanding the 2 execution phases explains hoisting, scope boundaries, closures, call stack overflows, and how the V8 engine manages memory.
Whenever a JavaScript program runs or a function is invoked, the engine creates an Execution Context in two distinct phases:
var set to undefined) and stores full function declarations.The JS engine manages execution contexts using a LIFO (Last-In, First-Out) Call Stack. The Global Execution Context (GEC) sits at the bottom of the stack. When a function is called, a new local Execution Context is pushed onto the stack. When the function returns, its context is popped off!
[ Call Stack ]
┌─────────────────────────┐
│ calculateTax() Context │ <- Currently executing (Top of stack)
├─────────────────────────┤
│ processOrder() Context │
├─────────────────────────┤
│ Global Context (GEC) │ <- Bottom of stack
└─────────────────────────┘
var x = 2;
function square(num) {
var ans = num * num;
return ans;
}
var square2 = square(x);
x: undefined, square: [function code], square2: undefined.x = 2. Calling square(2) creates a new local context.square local context: num = 2, ans = 4. return 4 pops local context off stack and assigns 4 to square2.RangeError: Maximum call stack size exceeded.Remember: Memory allocation happens BEFORE code execution. This leads directly to our next topic: Hoisting!