Scope determines where variables are accessible. Understanding the scope chain is essential for closures, hoisting, and debugging.
Types of Scope
Global Scope
Function Scope
Block Scope (let/const)
Lexical Scoping
JavaScript uses lexical (static) scoping — scope is determined by where code is WRITTEN, not where it's called.
The Scope Chain
Temporal Dead Zone (TDZ)
Module Scope
Common Gotchas
- Variables declared without var/let/const become global (sloppy mode)
- Function declarations are hoisted entirely; function expressions are not
- Block-scoped variables in loops create a new binding per iteration
- The scope chain is fixed at function creation, not execution