Lexical scope & the scope chain
Understand the idea
A helper prints the configuration from its defining even when another calls it with a same-named local variable. The caller does not decide lexical scope.
The code draws the rooms
The word lexical sounds more complicated than the idea: look at the written nesting of the code. It fixes which outer names a function can see. A caller cannot silently change those names just by declaring similarly named nearby.
Looking outward one scope at a time
A lookup happens whenever an needs the value associated with a name. If no accessible declaration supplies that name, JavaScript reports a ReferenceError. Keeping the lookup route fixed makes a helper behave consistently when several callers reuse it.
When JavaScript needs a name, it checks the current scope first, then its outer scope, then the next outer scope. That sequence is the scope chain. Finding a nearer declaration ends the search, even if that has not been initialized yet. It does not keep searching for a more convenient outer value.
| Place to look | What counts | What does not |
|---|---|---|
| Current scope | Local declarations and parameters | A different function’s locals |
| Enclosing scopes | Scopes that surround the definition | Scopes that only surround a later call |
| Module or global scope | Names accessible after looking outward | Private names in unrelated modules |
| Nearest matching declaration | Stops name lookup | An outer declaration hidden by that nearer name |
Definition versus call location
“Lexical” means the nesting in the source determines this relationship. It is not a search through whichever functions happen to be running. A caller's local variable does not become an input to the function it calls unless it is passed explicitly.
Shadowing and early access
Shadowing adds a nearer with the same name. It does not replace the outer binding. This is why a local declaration can unexpectedly hide a useful helper or built-in: lookup stops as soon as the nearer name matches, regardless of its type.
A stable meaning for reused functions
This fixed relationship lets functions remain understandable when moved between callers. It also explains : a function can continue using an enclosing binding after the defining call has returned. The binding remains connected to that function rather than being copied from a later caller.
In the example, runReport has a local region, but readRegion was defined outside runReport. Its lookup therefore reaches the outer EU binding. If the caller's local name could replace that lookup, a helper could change meaning every time a different caller reused it. Lexical scope keeps the helper's dependencies understandable from its definition.
Mental model
FOLLOW THE VALUE
The function has no region binding of its own.
01 / 03Trace the transformation
Live example
The same call prints EU from readRegion and US from runReport. The caller has not changed the helper’s chain.
Your console output appears here.
Guided challenge
Read the defining scope
Remove the hard-coded return and let readRegion resolve the region binding from its defining scope.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
EU US
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 150 XP. You can always revisit it.