Lesson 22 / 38

Scope & Hoisting

Where variables live, and why some are usable before their line.

Function vs block scope

A variable's scope is where it's visible: var is limited to the enclosing function, while let/const are limited to the enclosing { } block.

Hoisting

Declarations are moved to the top of their scope during compilation — but only the declaration, not the assignment.

console.log(x);   // undefined, not an error
var x = 5;

console.log(y);   // ReferenceError
let y = 5;

Temporal dead zone

let/const are hoisted too, but stay in a "temporal dead zone" until their line runs — accessing them earlier throws.