Fundamentals

var vs let vs const

How the three declarations differ in scope, hoisting and reassignment.

beginner Freshers

var, let and const all declare variables, but they behave differently and choosing the right one prevents a whole class of bugs.

Think of `const` as a name-tag glued to one seat — you can redecorate the seat, but the tag never moves to another seat.

Key concepts

1
Scope — var is function-scoped, so it leaks out of if/for blocks. let and const are block-scoped and only exist inside the nearest { }.
Scopevarifforlet
2
Reassignment — let can be reassigned, const cannot. Note that const only freezes the binding, not the value: a const object can still have its properties mutated.
Reassignmentletconst
3
Hoisting — var is hoisted and initialised to undefined. let and const are hoisted too but sit in the temporal dead zone until the declaration line, so reading them early throws a ReferenceError.
Hoistingtemporal dead zonevarundefinedlet