Sub topic 5
How the Scope Chain Resolves Names
This chapter assumes you know the basics of JavaScript syntax, can declare variables with var, let, and const, and can write nested functions. It also builds on the earlier chapters about global, function, and block scope. The question here is how JavaScript finds a variable when code uses its name.
The working model
Every scope keeps a record of the names declared directly inside it. Each scope also holds a link to its outer scope, which is the scope where its code was written. These links connect into a path called the scope chain, and the path ends at the global scope.
When code reads or writes a name, the engine follows these steps:
- Look in the current scope's record.
- If the name is not there, follow the link to the outer scope and look again.
- Repeat until the name is found or the global scope has been checked.
- If even the global scope does not have it, throw a
ReferenceError.
The search stops at the first match. Outer scopes are never searched once a match has been found.
Walking the chain
const level = "global";
function outer() {
const level = "outer";
function inner() {
return level;
}
return inner();
}
console.log(outer()); // "outer"
console.log(level); // "global"The inner function declares no level of its own. The engine then checks outer's scope, finds level there, and stops, so the global level is never consulted. The next chapter covers this situation in detail, where an inner name hides an outer one.
The chain is fixed where code is written
The scope chain is lexical: it comes from where a function sits in the source code, not from where it is called. This is the most common wrong assumption about scope.
const name = "global";
function show() {
return name;
}
function caller() {
const name = "caller";
return show();
}
console.log(caller()); // "global"show is called from inside caller, but show was written at the top level. Its outer link therefore points to the global scope, and caller's local name stays out of reach.
When lookup fails
If a read reaches the end of the chain without a match, JavaScript throws:
function f() {
try {
return missing;
} catch (e) {
return e.name;
}
}
console.log(f()); // "ReferenceError"A failed assignment in sloppy mode behaves differently: it can create a global variable without warning. A later chapter on accidental globals covers that case.
Common errors
| Mistake | What actually happens |
|---|---|
| Expecting a function to see the caller's local variables | Lookup follows the definition site, so the caller's locals are invisible |
| Assuming lookup gathers every match | The search stops at the nearest declaration |
| Expecting inner variables to be visible from outside | Links point outward only, so an outer scope cannot read an inner scope's names |
| Confusing `undefined` with "not found" | A name declared with the value `undefined` was found; a name that is not found throws `ReferenceError` |
Exercise
What does this code print, and why? const x = 1; function a() { return x; } function b() { const x = 2; return a(); } function c() { const x = 3; function d() { return x; } return d(); } console.log(b(), c());
Show answer
It prints "1 3". Function a was defined at the top level, so its chain goes straight to the global x (1), even though b called it. Function d was defined inside c, so its lookup reaches c's x (3) before it gets to the global scope.
Exercise
Inside a nested function, you read a variable named total that is declared nowhere in the file. Which scopes does the engine search, in what order, and what is the result?
Show answer
The engine searches the nested function's own scope, then each enclosing function scope from the inside out, then the global scope. When none of them declares total, the read throws a ReferenceError.