The Scope Chain

The full text of this chapter, to read or revisit. To practise, do the exercises.

▶ Do the exercises

Exercise 1 — The Scope Chain: Searching Outward

When you have scopes nested inside each other—like an `if` block inside a function, which is inside the global file—how does JavaScript find a variable? It starts in the innermost scope and searches outward step by step until it finds the variable:

const world = "Codéland"; function explore() { const area = "Tall Grass"; if (true) { const spot = "under a rock"; console.log(spot + " in " + area + " in " + world); } } explore();

When `console.log` runs inside the `if` block: 1. It finds `spot` right inside the `if` block. 2. It doesn't find `area` in the `if` block, so it steps out into `explore()` and finds `area` there. 3. It doesn't find `world` in `explore()`, so it steps out to the global scope and finds `world`. This step-by-step outward lookup is called the scope chain.

Exercise 2 — The Closest Variable Wins

When JavaScript walks outward along the scope chain, it stops at the very first match it finds. That means if an inner scope and an outer scope both have a variable with the same name, the inner code always uses the closest one:

const name = "Global Zap"; function show() { const name = "Local Zap"; console.log(name); } show(); console.log(name);

Inside `show()`, JavaScript immediately finds the local `name` (`"Local Zap"`) and stops searching. Outside `show()`, the global `name` (`"Global Zap"`) is completely untouched. If you remove the local `const name` inside `show()`, JavaScript doesn't find `name` locally and continues outward to the global `name`:

const name = "Global Zap"; function show() { console.log(name); } show();

Exercise 3 — When a Variable Is Nowhere in the Chain

What happens if JavaScript searches the current block, the enclosing function, and all the way out to the global scope—and still doesn't find the variable? It stops and throws a `ReferenceError: ... is not defined`:

function show() { console.log("Searching along the scope chain..."); console.log(missingVar); // Error! } show();

Remember the golden rule of the scope chain: lookups only travel outward, never inward! Outer code can never reach inside an inner function or block to grab its variables.

Exercise 4 — Quiz: Which One Wins?

What does this code print?

const x = "outer"; function show() { const x = "inner"; console.log(x); } show();
Options
• inner
• outer
• undefined
• An error

Hint : The search stops at the first declaration it meets.

Exercise 5 — Quiz: Reaching Outward

What does this code print?

const x = "outer"; function show() { console.log(x); } show();
Options
• outer
• undefined
• An error
• x

Hint : With nothing found locally, the search keeps walking outward.

Exercise 6 — Quiz: Three Levels Deep

What does this code print?

const v = 1; function outer() { const v = 2; function inner() { console.log(v); } inner(); } outer();
Options
• 2
• 1
• undefined
• An error

Hint : Start from the innermost area and walk out until a match appears.

Exercise 7 — Reach the Outer Value

Make the function print the outer name instead of declaring its own.

Hint : Use the variable itself, without quotes, and then run the function.

Exercise 8 — Scope Chain Cheat Sheet

Match each statement with what it describes.

Items to match
• The direction of the search
• When a name is found
• Two variables with the same name
• Nothing found anywhere
Definitions
• From inside to outside
• The search stops there
• The closest one wins
• An error

Exercise 9 — Quiz: Looking Inward

What happens on the last line?

function hide() { const secret = "hidden"; } hide(); console.log(secret);
Options
• An error
• "hidden"
• undefined
• Nothing

Hint : The chain is searched outward only.

Exercise 10 — Three Levels of Data

Build a small program with three nested levels and show exactly which value each level sees.

Step 1: declare a global `region` worth `"Codéland"` and a global `level` worth `1`. Step 2: `explore()` declares its own `level` worth `2`, then prints `"In explore: region / level"` using both names. Step 3: inside `explore`, an inner function `search()` declares nothing of its own and prints `"In search: region / level"`. Step 4: call `search` from inside `explore`, call `explore`, then print `"Outside: region / level"` to show the global is untouched.

Expected output:

In explore: Codéland / 2 In search: Codéland / 2 Outside: Codéland / 1
Starter code
// Write your code here
Back to the course