Skip to content
engineering workbench.by StudyPort Academy
Learning path
Intermediate20 minLESSON 12 / 34

Lexical scope & the scope chain

01

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.

Where name lookup does and does not look
Place to lookWhat countsWhat does not
Current scopeLocal declarations and parametersA different function’s locals
Enclosing scopesScopes that surround the definitionScopes that only surround a later call
Module or global scopeNames accessible after looking outwardPrivate names in unrelated modules
Nearest matching declarationStops name lookupAn 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.

02

Mental model

FOLLOW THE VALUE

readRegion body
local lookup
no local region

The function has no region binding of its own.

01 / 03Trace the transformation

03

Live example

The same call prints EU from readRegion and US from runReport. The caller has not changed the helper’s chain.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022
04

Guided challenge

Read the defining scope

Remove the hard-coded return and let readRegion resolve the region binding from its defining scope.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

EU
US

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01What determines a function’s outer scope?
    02When does name lookup stop?

    Ready to call this one understood?

    Mark this lesson complete to earn 150 XP. You can always revisit it.

    Sign in to save progress
    Next lesson