Everything in JavaScript happens inside an Execution Context. If you aspire to master JavaScript at a senior engineering level or score top marks in university computer science exams, understanding how the V8 engine creates execution contexts and manages the Call Stack is mandatory.
Figure 11: The Two Phases of Execution Context: Memory Creation Phase vs Code Execution Phase, and LIFO Call Stack mechanics.
Before running a single line of executable code, the engine scans the script and allocates memory space for all declarations:
var are allocated and assigned undefined.let and const are allocated in the Temporal Dead Zone (TDZ) without initialization.this keyword is bound.JavaScript executes the program line-by-line, assigning real values to variables (e.g. score = 95) and invoking functions.
var x = 10;
function calculateDouble(num) {
var result = num * 2;
return result;
}
var y = calculateDouble(x);
console.log(y); // 20
| Step | Call Stack State | Action Performed |
|---|---|---|
| 1 | [Global Execution Context] | GEC created. x = undefined, calculateDouble = fn(), y = undefined. |
| 2 | [Global Execution Context] | Line 1: x = 10. |
| 3 | [FEC: calculateDouble][Global Execution Context] | Line 8: calculateDouble(10) invoked. New FEC pushed on top of Call Stack. num = 10, result = undefined. |
| 4 | [FEC: calculateDouble][Global Execution Context] | Line 4: result = 20. Returns 20 to caller. |
| 5 | [Global Execution Context] | FEC popped off stack and garbage-collected. y = 20. |
| 6 | [Empty Stack] | Script ends. GEC popped off. |
Exam Question: "What causes a 'Maximum call stack size exceeded' RangeError?"
Answer: Known as a Stack Overflow, this occurs when recursive functions invoke themselves indefinitely without a proper base termination condition. Each recursive call pushes a new Function Execution Context onto the Call Stack until the browser's allocated stack memory limit (typically ~10,000 frames) is breached.
How do inner functions remember variables from outer scopes even after the parent function has finished executing? Proceed to: JavaScript Scope Chain & Closures →