A conceptual map of scope, hoisting and the TDZ
Three connected ideas explain why a variable works in one place and breaks in another.
Scope defines where you can access a variable. Hoisting explains which declarations are processed before line-by-line execution begins. The temporal dead zone marks the stretch where `let` and `const` already exist but still cannot be used.
If you understand this trio, your errors stop feeling random and become fully traceable.
Global scope
Visible from the whole file or module.
- Useful for shared configuration
- Dangerous when overused because it couples too much code
Function scope
Variables live only inside the function.
- Encapsulates logic
- Reduces side effects
Block scope
With `let` and `const`, the variable lives inside `{}`.
- `if`, `for` and `while` create blocks
- It prevents accidental variable leaks
TDZ
The temporal dead zone of `let` and `const`.
- The binding exists before initialization
- Reading it too early causes `ReferenceError`