What is lexical scope and the scope chain in JavaScript?
BeginnerJavaScript interview questionBy UIRound, updated 17 November 2026
Short answer
Lexical scope means the variables a piece of code can see are decided by where that code is written in the source, not by where it is called from. Scopes nest: global, then functions, then blocks. When you use a name, JavaScript looks in the current scope, then the one around it, and so on outward to the global scope. That ordered list of scopes is the scope chain.
The kinds of scope
A scope is a region of code that owns a set of names. JavaScript has four you meet every day.
| Scope | Created by | What lives there |
|---|---|---|
| Global | The top level of a classic script | var and function declarations (also added to globalThis), plus top-level let, const and class (not added to globalThis) |
| Module | Each ES module file | Everything declared at the module's top level. Nothing leaks to the global scope |
| Function | Every function call | Parameters, var, let, const and inner function declarations |
| Block | Any { }: if, for, while, try, a bare block |
let, const and class only. var ignores blocks |
The block rule is the one that catches people. A var inside an if belongs to the enclosing function, so it is visible after the block. A let inside the same if disappears when the block ends. The full comparison of the three keywords is in var vs let vs const.
catch (err) also creates a small scope for err, and a for (let i = ...) loop creates a fresh binding of i for every iteration, which is why the classic loop bug goes away with let.
How lookup walks outward
Each scope keeps a link to the scope it was written inside. Resolving a name means following those links until one of them has the name.
const app = "shop";
function outer() {
const user = "Asha";
function inner() {
const item = "book";
console.log(item, user, app); // book Asha shop
}
inner();
}
outer();To find app, the engine checks inner's scope, then outer's, then the global scope, and stops at the first match. The search only goes outward. outer cannot see item, and two sibling functions cannot see each other's locals.
If the name is not found anywhere, reading it throws ReferenceError: x is not defined. Assigning to an undeclared name is worse: in sloppy mode it silently creates a global variable, and in strict mode it throws. typeof undeclaredName is the one read that does not throw; it returns "undefined".
Lexical, not dynamic
"Lexical" means the chain is fixed by the source text. It does not change based on the caller.
const level = "global";
function report() {
return level; // resolved where report is written
}
function run() {
const level = "inside run";
return report();
}
run(); // "global"report was written at the top level, so its outer scope is the global scope, whatever calls it. A language with dynamic scope would return "inside run". This fixed link is exactly what lets a function keep using its outer variables after the outer function has returned, which is a closure.
One thing in JavaScript does depend on the call: this in a regular function. It is not part of the scope chain. Arrow functions are the exception, because they take this lexically from the scope around them. See how this works.
Shadowing
When an inner scope declares a name that already exists further out, the inner one wins inside that scope. The outer variable is untouched; it is just hidden.
let total = 10;
function addTax(total) {
// the parameter shadows the outer total
return total * 1.2;
}
addTax(5); // 6
total; // 10Shadowing is legal and sometimes useful, but it has two traps.
let count = 1;
{
console.log(count); // ReferenceError: Cannot access 'count' before initialization
let count = 2;
}The inner let count exists from the start of its block, so it shadows the outer one even on the line above its declaration. That is the temporal dead zone.
let id = 1;
{
var id = 2; // SyntaxError: Identifier 'id' has already been declared
}var ignores the block and lands in the outer scope, where let id already lives. The opposite order, an outer var and an inner let, is allowed.
Why it matters in real code
- Predictable code. You can work out what a name refers to by reading the file, without tracing every caller.
- Privacy. Variables inside a function or module cannot be touched from outside unless you return or export them.
- Engine optimisation. Because most names can be resolved from the source, engines can find variables quickly.
evalandwithcan add names at runtime, which is one reason both are avoided (andwithis banned in strict mode).
Practise it
Start with Predict scope puzzles and Predict closure results, which test the lookup rules on small snippets. Then Fix the var loop closure bug three ways, Predict hoisting and TDZ results and Predict which function declaration wins cover block scope and shadowing.
Common follow-up questions
What is the difference between scope and the scope chain?
A scope is one region and its names. The scope chain is the ordered list of scopes the engine searches for a given piece of code: its own scope first, then each enclosing scope, ending at the global scope.
Is scope the same as execution context?
No. Scope is about which names are visible, and it comes from the source. An execution context is the runtime record of one running piece of code, including its variables and its this. Each context points to the scope it runs in. The call stack and execution context explains the runtime side.
Do parameters have their own scope?
Parameters live in the function's scope. When a function has default parameter values, the parameters get a separate scope that sits outside the body, so a default value cannot see variables declared inside the function body.
Are top-level let and const global variables?
let and const global variables?They are in the global scope of a classic script, so every script on the page can read them. They are not properties of window or globalThis, unlike top-level var. In an ES module, top-level declarations stay private to the module.
Mistakes to avoid
Assuming a function can see the variables of whoever called it
Only the place it was written counts.
Using
varand expecting it to stay inside aniforforblockAssigning to a misspelled name in sloppy mode and creating a global by accident
Shadowing an outer variable with a same-named
letand then reading it above the declarationThinking
thisfollows lexical scope in regular functions
Practise it in code
Interviewers rarely stop at the definition. These questions open in an editor with tests.
- Predict scope puzzles: named function expressions, the
catchparameter, default-parameter scope and shadowingEasy25 min - Predict closure results: counters, loops, captured variables and lexical scopeEasy20 min
- Fix the
varloop closure bug three waysEasy15 min - Predict hoisting and TDZ results for functions,
var,let,constandclassEasy15 minFree - Predict which function declaration wins: duplicates, code after
returnand functions inside blocksEasy15 min