What is the difference between let, const, and var?
Beginneres6scopehoistingvariables
Full Answer
JavaScript has three ways to declare variables: var, let, and const. They differ in scope, hoisting behavior, and mutability.
var
- Function-scoped (or globally scoped if declared outside a function)
- Hoisted to the top of its scope and initialized as
undefined - Can be redeclared and reassigned
let
- Block-scoped (limited to the
{}block it is declared in) - Hoisted but NOT initialized — accessing it before declaration throws a
ReferenceError(Temporal Dead Zone) - Cannot be redeclared in the same scope, but can be reassigned
const
- Block-scoped, same as
let - Cannot be redeclared or reassigned — the binding is constant
- Objects and arrays declared with
constare still mutable (their contents can change)
Rule of thumb: prefer const by default, use let when you need to reassign, avoid var.
Quick Answer for Interviewer
var is function-scoped and hoisted as undefined; let and const are block-scoped with a Temporal Dead Zone. const prevents reassignment; let allows it. Prefer const by default.
Flashcard