Back
JavaScriptMediumNew
Explain lexical scoping. How does it differ from dynamic scoping?
Scope is determined by where code is written, not where it is called.
Ideal Answer
Lexical scoping (static scoping) means a variable's scope is determined by where the function is defined in the source code, not where it is invoked.
JavaScript
const x = 'global'
function outer() {
const x = 'outer'
function inner() {
console.log(x) // "outer" — resolved lexically
}
return inner
}
const fn = outer()
fn() // still "outer", not "global"
JavaScript uses only lexical scoping. The engine walks the scope chain outward from the innermost environment at write time.
Dynamic scoping (not in JS) would resolve variables based on the call stack at runtime — e.g. if inner() looked up x from whoever called it.
Why it matters
- Enables closures — inner functions reliably capture outer bindings.
- Explains
thisvs scope —thisis dynamic (call-site), variable lookup is lexical (definition-site). - Arrow functions inherit lexical
thisfrom their enclosing scope, unlike regular functions.
Languages with dynamic scoping (e.g. some Lisp dialects, Bash) behave differently; JavaScript developers often confuse this rules with scope rules — keep them separate in interviews.