Module 6 · Scope, Hoisting and Closures
Every Rule, One by One: Blocks, var, Hoisting and the Dead Zone
Free with an accountReading
Bob's shop program prints notebook costs undefined. He set the price to 450 on line 9, and line 3 printed it. No error stopped him, because he declared the price with var, and var makes reading a name early legal. Module 1 promised to explain this, and the hoisting section below does.
Checking what this lesson needs for you…
What is inside Every Rule, One by One: Blocks, var, Hoisting and the Dead Zone
- The file's scope and the global object
- Block scope: let and const stay in their braces
- Function scope: var ignores blocks
- Parameters: the function's own names
- Shadowing: an inner name hides an outer one
- The scope chain and "is not defined"
- Hoisting: two passes over every scope
- The temporal dead zone
- Function expressions and arrows hoist as their variable
- A for loop's let: a fresh binding on every pass
- The IIFE: a scope made by hand