Block/Function/Module Scope and the TDZ
Understand block, function, and module scope; see TDZ in action and avoid leaking variables.
Intro
Goal: Know where variables live and when they are usable.
- Block scope (let/const)
- Function scope (var)
- Module scope
- TDZ safety

Block scope demo
Block scope keeps variables local to a pair of braces.
// Block scope: let/const exist only inside the braces
let lives = 3;
if (true) {
const bonus = 5;
let level = 1;
console.log("Inside block:", bonus, level);
}
console.log("Lives:", lives);
// Uncommenting next line would fail (not in scope):
// console.log(bonus, level);

All lessons in this course
- let, const, and avoiding var
- Block/Function/Module Scope and the TDZ
- Naming, Readability, and Common Pitfalls