Scope
Scope is the region of code where a name can be used. JavaScript has module, function, and block scopes, and a name resolves by walking outward from the innermost one.
Detailed Explanation
A binding is a name paired with a value. Scope is the rule that says which bindings are visible at a given line. If the engine cannot find a name in the current scope, it looks at the enclosing scope, then the next, until it reaches the global object or the module. That walk is lexical: it follows where the code was written, not where the function was called.
Three scopes matter in modern JavaScript.
Module scope. Each file that is a module has its own top-level scope. const x = 1 at the top of a module is not a property of globalThis. Other files see it only if you export it.
Function scope. var, function declarations, and the arguments object belong to the enclosing function. A var inside a for or if is still visible throughout that function. This is the behaviour that made var infamous.
Block scope. let, const, and class belong to the nearest { ... }: a block, a loop, a catch, a switch. Two let i in two for loops in the same function are two bindings. A catch (err) parameter is block-scoped to the catch.
Global scope still exists. In a browser classic script (not a module), a top-level var or function becomes a property of window. Top-level let and const do not. In Node CommonJS, each file is wrapped in a function, so even var at the top of a file is not global unless you assign global.x. In Node ESM, top-level bindings are module-scoped.
Shadowing happens when an inner scope reuses a name. The inner binding hides the outer one for the duration of the inner scope. There is no way to reach the outer name except by not shadowing it. globalThis is the escape hatch for actual globals, not for module bindings.
with and eval can add or change scopes at run time. with is forbidden in strict mode. eval of a local var in sloppy mode leaks into the enclosing function. Treat both as historical.
What it costs you: looking in the wrong scope. A var inside an if is not private to the if. A let inside a loop is a new binding each iteration, which is why let plus setTimeout works and var plus setTimeout does not. Accidental globals (x = 1 without a declaration, in sloppy mode) write to globalThis and survive the function.
Reach for the smallest scope that is correct. Prefer const, then let. Keep var out of new code.
Code Examples
const tax = 0.1; // module or script scope in this snippet
function withTax(amount) {
const rate = tax; // finds tax in the outer scope
if (amount > 0) {
const total = amount * (1 + rate); // block-scoped
return total;
}
// console.log(total); // ReferenceError: total is not defined
return 0;
}
console.log(withTax(100)); // => 110function leaky() {
if (true) {
var hidden = "not actually hidden";
}
return hidden; // var is function-scoped
}
console.log(leaky()); // => not actually hiddenfunction timers() {
for (var i = 0; i < 3; i++) {
setTimeout(() => {}, 0);
}
// one `i`, value 3 after the loop
for (let j = 0; j < 3; j++) {
// a fresh `j` per iteration
}
}
timers();Common Gotchas
`var` ignores blocks
an `if` or `for` does not contain it. Use `let` or `const` when you want the name to die with the block.
Reading `let`/`const` before their line is a `ReferenceError`
the binding exists but is in the temporal dead zone. `typeof` does not save you.
Undeclared assignment creates a global in sloppy mode
`"use strict"` or modules turn that into a `ReferenceError`.
Modules are not the global object
top-level `const` in a module is invisible to `window` / `global` and to other files unless exported.