Skip to content
YouTurkey Tech
All JavaScript topics

Scope Chain

Intermediate

How JavaScript finds a variable by searching from the current scope outward.Builds on Hoisting

Call stack

Empty

Scopes

Empty

Heap

Empty

Console

Empty

0/17Press Play, or step through with the arrows, to begin.
Step 0 of 17

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:

  1. Look in the current environment.
  2. Not there? Follow the outer reference and look again.
  3. 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 written

Quick 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
  1. Step 1:

    The global context starts. Its environment is the outermost scope, so its outer reference is null.

  2. Step 2:

    Creation phase: planet is reserved but uninitialized, as every const starts.

    Line 1

  3. Step 3:

    outer is stored as a function object. JavaScript remembers it was written in the global scope.

    Lines 3–12

  4. Step 4:

    Execution begins: planet is initialized to 'Earth'.

    Line 1

  5. Step 5:

    Calling outer() creates its environment. Its outer reference points to the global environment, where outer was written.

    Line 14

  6. Step 6:

    In outer's creation phase, city is reserved and inner is stored as a function.

    Lines 4–9

  7. Step 7:

    outer starts executing: city is initialized to 'Cairo'.

    Line 4

  8. Step 8:

    Calling inner() creates its environment, linked to outer's because inner was written inside outer.

    Line 11

  9. Step 9:

    inner declares street and initializes it to 'Nile St'.

    Line 7

  10. Step 10:

    To print, JavaScript looks up each name. street is found right away, in inner's own environment.

    Line 8

  11. Step 11:

    city is not in inner's environment, so the lookup follows the outer reference and finds it in outer's.

    Line 8

  12. Step 12:

    planet takes one more hop, all the way to the global environment.

    Line 8

  13. Step 13:

    All three names were found, so the line prints Nile St Cairo Earth.

    Line 8

  14. Step 14:

    inner returns. Its context leaves the stack and its environment is discarded.

    Line 9

  15. Step 15:

    outer returns too. Its environment is discarded, along with city and the inner function.

    Line 12

  16. Step 16:

    From the global scope the chain has a single link, and city is not in it. The lookup fails with a ReferenceError.

    Line 15

  17. Step 17:

    Lookups only go outward, never into inner scopes. The uncaught error ends the script.

    Line 15