Execution Context & Call Stack Deep Dive
Execution Context & Call Stack Deep Dive in Modern JavaScript
To understand how JavaScript executes code, handles hoisting, resolves scopes, and maintains asynchronous call stacks, one must understand the internal mechanics of the Execution Context.
1. What is an Execution Context?
An Execution Context (EC) is an abstract environment created by the JavaScript engine to evaluate and execute code. There are three types:
- 1Global Execution Context (GEC): Created once when the script loads. Sets up
window(browser) orglobal(Node.js) andthis. - 2Function Execution Context (FEC): Created anew whenever a function is invoked.
- 3Eval Execution Context: Created by code evaluated inside
eval()(rarely used).
2. The Two Phases of Execution Context
Every Execution Context is created in two distinct phases:
3. Hoisting & The Temporal Dead Zone (TDZ)
During the Creation Phase, declarations are registered in memory before code executes:
- Function Declarations: Fully hoisted into memory. Can be called before their definition!
var: Hoisted and initialized withundefined.letandconst: Hoisted but uninitialized. They enter the Temporal Dead Zone (TDZ) from the start of the block until their declaration line is executed.
4. The Call Stack (LIFO: Last In, First Out)
The Call Stack is the data structure that manages execution contexts during runtime:
5. Stack Overflow
If functions call each other recursively without a termination condition, the Call Stack exceeds its memory allocation limit, triggering a RangeError: Maximum call stack size exceeded:
Practice Quiz
Scope Chain & Lexical Environment
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Memoization & Pure Functions | Scope Chain & Lexical Environment |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.