Scope Chain
IntermediateHow JavaScript finds a variable by searching from the current scope outward.Builds on Hoisting
Empty
Empty
Empty
Empty
Spaceplay or pause←→previous or next stepRback to the start
Explanation
2 min read
On this page
What is the scope chain in JavaScript?
A scope is the region of code where a name is visible. Scopes nest: a function written inside another function sits inside its scope, and everything sits inside the global scope. When code uses a name, JavaScript searches the current scope first, then each enclosing scope in turn, until it finds it. That path is the .
How does JavaScript find a variable?
Every environment keeps a reference to its outer environment: the one where its code was written, not where it was called from. This is called lexical scope. A lookup follows those references like links in a chain:
- Look in the current environment.
- Not there? Follow the outer reference and look again.
- Reached the global environment and still nothing? Throw a
ReferenceError.
Lookups only go outward. An outer scope can never see into an inner one.
What mistakes do people make with scope?
Mini example
const level = 'global'; function show() { console.log(level);} function run() { const level = 'run'; show();} run(); // 'global': show looks where it was writtenQuick check
What does run() log in the mini example?
Further reading
Watch the video
The full explanation from the YouTurkey Tech YouTube channel, in Arabic.
All steps as text
- Step 1:
The global context starts. Its environment is the outermost scope, so its outer reference is null.
- Step 2:
Creation phase:
planetis reserved but uninitialized, as everyconststarts.Line 1
- Step 3:
outeris stored as a function object. JavaScript remembers it was written in the global scope.Lines 3–12
- Step 4:
Execution begins:
planetis initialized to 'Earth'.Line 1
- Step 5:
Calling
outer()creates its environment. Its outer reference points to the global environment, whereouterwas written.Line 14
- Step 6:
In outer's creation phase,
cityis reserved andinneris stored as a function.Lines 4–9
- Step 7:
outer starts executing:
cityis initialized to 'Cairo'.Line 4
- Step 8:
Calling
inner()creates its environment, linked to outer's becauseinnerwas written insideouter.Line 11
- Step 9:
inner declares
streetand initializes it to 'Nile St'.Line 7
- Step 10:
To print, JavaScript looks up each name.
streetis found right away, in inner's own environment.Line 8
- Step 11:
cityis not in inner's environment, so the lookup follows the outer reference and finds it in outer's.Line 8
- Step 12:
planettakes one more hop, all the way to the global environment.Line 8
- Step 13:
All three names were found, so the line prints Nile St Cairo Earth.
Line 8
- Step 14:
inner returns. Its context leaves the stack and its environment is discarded.
Line 9
- Step 15:
outer returns too. Its environment is discarded, along with
cityand theinnerfunction.Line 12
- Step 16:
From the global scope the chain has a single link, and
cityis not in it. The lookup fails with a ReferenceError.Line 15
- Step 17:
Lookups only go outward, never into inner scopes. The uncaught error ends the script.
Line 15