The Scope Chain in JavaScript: How Variable Resolution Works
The scope chain is how JS finds variables. Here is how the engine walks the chain from local to global.
The Scope Chain in JavaScript: How Variable Resolution Works
The scope chain is the linked list of lexical environments that the engine walks to resolve a variable. It is built at function definition time, not call time.
How Resolution Works
When the engine needs a variable:
- Look in the current lexical environment's environment record.
- If not found, follow the outer reference to the parent environment.
- Repeat until found or the global environment is reached.
- If not found anywhere, throw
ReferenceError.
Example
const globalVar = "G"; function outer() { const outerVar = "O"; function inner() { const innerVar = "I"; console.log(innerVar, outerVar, globalVar); // I O G } inner(); } outer(); `` When `inner` runs and looks up `outerVar`: - `inner`'s environment record: has `innerVar`, not `outerVar`. - Follow outer reference to `outer`'s environment: has `outerVar`. Found. If it needed `globalVar`: - `inner` -> `outer` -> `global`. Found at the top. ### The Chain Is Lexical The chain is determined by where functions are **written**, not where they are **called**: ```js function inner() { console.log(x); } function outer() { const x = 10; inner(); } const x = 99; outer(); // 99, not 10 `` `inner` was written in the global scope, so its chain is `inner -> global`. Calling it from inside `outer` does not change that. ### The Chain and Closures If a function is returned, it keeps its outer references. The chain persists: ```js function makeAdder(x) { return function (y) { return x + y; // x is found via the chain, even after makeAdder returns }; } const add5 = makeAdder(5); add5(3); // 8 `` ### Shadowing A local variable with the same name as an outer one shadows the outer: ```js const name = "Kunal"; function greet() { const name = "Asha"; console.log(name); // "Asha" (local shadows global) } `` The engine finds the local first and stops; it never reaches the global. ### The Takeaway The scope chain is the linked list of lexical environments the engine walks to resolve variables. It is built lexically (at write time). The engine looks locally first, then follows outer references up to the global scope. Shadowing stops the search early. Closures work because the chain persists after the outer function returns.
The linked list of lexical environments that the engine walks to resolve a variable. It looks in the current environment first, then follows outer references up to the global scope. If not found, it throws ReferenceError.
At definition time. The outer references are determined by where functions are written (lexical scope), not where they are called. This is why a function called from a different scope does not gain access to that scope's variables.
A local variable with the same name as an outer one shadows the outer. The engine finds the local first and stops searching, so the outer variable is hidden but not affected.
A closure keeps its scope chain (outer references) even after the outer function returns. This lets the inner function access the outer function's variables later. The chain persists because the lexical environment is retained.
It throws ReferenceError: x is not defined. This means the variable does not exist in any accessible lexical environment, from the current scope up to the global scope.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

