What is the difference between var, let and const?

JavaScript Functions & Scope Basic Fresher1–2 Yrs Conceptual 1 min read

Quick Answer

var is function-scoped, hoisted and initialised to undefined, and can be redeclared. let and const are block-scoped and hoisted but not initialised (the “temporal dead zone” until the declaration). const also can't be reassigned — though a const object's properties can still change. Use const by default, let when you must reassign, and avoid var.

var is function-scoped and gets hoisted to the top of the function as undefined, so you can reference it before the line it's declared on. let and const are block-scoped — they only exist inside the nearest braces — and although they're hoisted, you can't use them before the declaration; that gap is the temporal dead zone. const on top of that can't be reassigned, but that only locks the binding, not the contents, so you can still push to a const array. My rule is const everywhere, let when I genuinely need to reassign, and never var.

varletconst
ScopeFunctionBlockBlock
HoistingYes → undefinedYes → TDZYes → TDZ
ReassignYesYesNo
Redeclare in same scopeYesNoNo

const prevents reassigning the binding, not mutating the value: const a = []; a.push(1) is fine, a = [2] throws. Block scoping is why let fixes the classic for-loop closure bug.