What is lexical scope and the scope chain?
Quick answer
Lexical scope means a function's accessible variables are decided by where it's written. The scope chain is the outward lookup path the engine follows.
In detail
A function can see variables in its own scope and every enclosing scope, decided at definition time (not call time). When a variable is read, the engine checks the current scope, then the parent, up to global — the first match wins. This is exactly the mechanism that makes closures possible.
1const g = "global";
2function outer() {
3 const o = "outer";
4 function inner() { return g + o; } // reaches up the chain
5 return inner();
6}Why interviewers ask this: Foundational — closures and `this` build on it.
This is 1 of 118+ questions in the JavaScript Interview Kit
Get every question with detailed answers, follow-ups and real code — plus coding challenges and a last-minute revision sheet. One-time payment, instant access.
⚡ Get the JavaScript Interview Kit → ₹299