Scope Chain

Med
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Scope Chain

Approach

Nest functions to demonstrate lexical scope chain lookup. The inner function accesses its own variables, then the enclosing outer function variables, then global variables. Variable shadowing shows that a local declaration hides the outer variable of the same name without modifying it.

Complexity Analysis

Time
O(1)
Space
O(1)

Pattern

Lexical Scope Chain

Why It Works

JavaScript resolves variable references by walking outward through the lexical scope chain, stopping at the first match. Shadowing creates a new binding in the inner scope, leaving the outer binding untouched.

Updated Feb 2026