Handbooks / JavaScript / Chapter 2
Scope and Closures
7 pages · ~5 min✓ Reviewed
Builds on Types and Coercion. Next up: this and Prototypes.
Variables and Scope Mental Model
1. var, let, const
| var | let | const | |
|---|---|---|---|
| Scope | function | block | block |
| Reassign | yes | yes | no |
- A block is any
{ }:if,for, or a bare block constlocks the binding, not the value
javascript
const a = []; a.push(1); // works a = []; // TypeError
mutating is fine, rebinding throws
2. Lexical scope and lookup
- Lexical = visibility fixed by where code is written
- Not by where the function is called
- Current scope
- Enclosing scopesoutward
- Global
- ReferenceErrornot found
3. Global object
| Global declaration | On globalThis? |
|---|---|
var | yes |
| function declaration | yes |
let / const | no |
Remember
Top-level let x = 1 gives globalThis.x as undefined; only var and functions attach.
Hoisting and the Temporal Dead Zone
1. What hoisting does
- Hoisting: declarations are registered at scope creation, before code runs
- The name exists from the top of its scope
varis hoisted and initialised to undefined
javascript
console.log(x); var x = 5;
no error: x exists, value undefined
output
undefined2. let, const and functions
let/constare hoisted but uninitialised- Access before the declaration line → ReferenceError
- That gap is the TDZ (temporal dead zone)
Closures: How They Work and Where to Use Them
1. What a closure is
- Closure = function + reference to the scope where it was created
- Keeps that scope alive after the outer function returns
- Captures by reference, not value: later changes are seen
2. Private state and factories
nis private: only the returned function can touch it- Factory: outer arg fixed now, inner arg later (partial application)
javascript
function counter(){ let n = 0; return () => ++n } const c = counter(); console.log(c(), c()); function mul(a){ return b => a*b } const double = mul(2); console.log(double(5), c.n);
output
1 2 10 undefined
3. Common uses
The Loop Closure Pitfall
1. The Bug
- All callbacks share one function-scoped
i - Callbacks run later, after the loop ends, when
iis 3
javascript
for (var i = 0; i < 3; i++) { setTimeout(() => console.log(i)) }
output
3 3 3
2. Three Fixes
| Fix | How | Prints |
|---|---|---|
let in loop head | fresh binding copy per iteration | 0 1 2 |
| IIFE (legacy) | (function(j){ setTimeout(()=>console.log(j)) })(i) |
IIFE and Modules
1. IIFE and module pattern
(function(){ ... })();runs once, private scope- Wrapping
( )make it an expression, not a declaration - Module pattern: IIFE returns public methods; vars stay private via closures
javascript
const counter = (function () { let n = 0; return { inc: () => ++n }; })(); counter.inc(); // n is unreachable
2. ES modules
- Own module scope: top-level vars are not global
- Strict mode by default
- Loaded once, then cached
- Exports are live bindings: importers see new values of an exported
let - Importers cannot assign to an import
Scope and Closures Cheat Sheet
1. Declarations
| var | let | const | |
|---|---|---|---|
| Scope | function | block | block |
| Hoisted as | undefined | TDZ | TDZ |
| Redeclare | yes | no | no |
| Reassign | yes | yes | no |
Check yourself
What does this print?
- 3 3 3
- One function-scoped
iis shared and is 3 when the callbacks run - With
let i, each iteration gets its own binding: 0 1 2
for (var i = 0; i < 3; i++) setTimeout(() => console.log(i))
Why does this throw?
- ReferenceError
yis in the temporal dead zone until its declaration linetypeofis only safe on names never declared
console.log(typeof y) let y = 1
Which line throws, and what error?
- Only the last line: TypeError
pushmutates the value, and the binding stays the samea = []reassigns the binding, whichconstforbids