› Module 5: Scope & Execution › 5.1 Scope & the Scope Chain
M O D U L E

Module 5: Scope & Execution

Where variables live, and how code actually runs

5.1 Scope & the Scope Chain

Scope establishes the accessibility and lifetime of variables in code. JavaScript enforces lexical scoping (static scoping): the scope of an identifier is established by its author-time source code block nesting.

Figure 5.1: The Scope Chain — Nested Lexical Environments Global Scope (var a = 10) Function outer() Scope (var b = 20) Function inner() Scope (var c = 30) • Can access c, b, AND a! (Lookups move outward)

Figure 5.1: Scope lookup moves strictly outward from innermost scope to outer scopes until it reaches the global object, never inward.

5.2 Execution Context

An Execution Context is the internal runtime environment created by the JavaScript engine whenever code runs. It contains local variables, the scope chain, and the this binding for that specific execution frame.

Figure 5.2: The Two Phases of an Execution Context Phase 1: Creation (Memory Allocation) • Scans variable & function declarations • Sets up Scope Chain reference • Determines 'this' binding Phase 2: Execution (Code Evaluation) • Assigns runtime values to variables • Executes functions line-by-line • Manages call stack push/pop

Figure 5.2: Every execution context undergoes a Creation Phase (memory allocation & hoisting) followed by an Execution Phase (evaluation & assignment).

5.3 The Temporal Dead Zone

The Temporal Dead Zone (TDZ) is the specific temporal interval between the start of a scope and the line where a let or const variable declaration is evaluated in memory.

Figure 5.3: Lifecycle of a let/const Variable and the Temporal Dead Zone (TDZ) 1. Scope Entered Variable Binding Created [ In TDZ • Unusable ] 2. Accessing Before Init console.log(val); → Throws ReferenceError! 3. Declaration Reached let val = 10; [ TDZ Ends • Safe! ]

Figure 5.3: Accessing a variable while in the TDZ throws a ReferenceError, preventing undefined bugs.

MODULE COMPREHENSIVE QUIZ

Module 5 Final Quiz

Test your understanding across all 3 topics of this module with interview-grade questions, tricky edge cases, and code output predictions.

Take Module 5 Final Quiz →
Yukti Mascot