Lesson 23 / 40

Scope & Hoisting

Understand global, function and block scope, hoisting and the temporal dead zone for let and const.

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.