var is function-scoped and hoisted with undefined, let is block-scoped and reassignable, const is block-scoped and cannot be reassigned — modern JavaScript favors const by default and let when reassignment is needed.
Function-scoped, hoisted with undefined, can be re-declared — causes scoping bugs in loops and blocks
Block-scoped, hoisted but in TDZ, can be reassigned but not re-declared in the same scope
Block-scoped, must be initialized, cannot be reassigned — but object/array contents can still be mutated
var ignores block boundaries (if/for); let and const are confined to the nearest {} block
Default to const, use let for reassignment, avoid var — this prevents scoping bugs and signals intent
JavaScript has three variable declaration keywords: var (original), let and const (added in ES6/ES2015). Understanding their differences in scope, hoisting, and mutability is fundamental and one of the most common interview questions.
var declared inside a function is accessible anywhere within that function, regardless of block boundaries (if, for, etc.).var creates a global variable and adds a property to the global object (window in browsers).var x = 1; var x = 2; is valid.undefined — accessing before declaration returns undefined instead of throwing an error.var in a for loop is shared across all iterations, which breaks closures inside the loop.{} — if, for, while, or any curly-brace block).let x = 1; let x = 2; throws SyntaxError.let x = 1; x = 2; is valid.ReferenceError (Temporal Dead Zone).let x; is valid (value is undefined until assigned).let.const x; throws SyntaxError.const x = 1; x = 2; throws TypeError.const prevents reassignment of the binding, not mutation of the value. A const object or array can still have its properties/elements modified: const arr = [1]; arr.push(2); is valid.Object.freeze() — but this is shallow (nested objects are still mutable).const by default — it signals that the binding won't change, making code easier to reason about.let only when you need to reassign (loop counters, accumulators, conditional assignments).var entirely in modern code — it causes scoping bugs and is hoisted in confusing ways.Key Interview Distinction: const Does Not Mean Immutable
const prevents reassignment of the variable binding, not mutation of the value. const obj = {}; obj.key = 'value'; works fine — you're mutating the object, not reassigning obj. This is the most commonly misunderstood aspect and a frequent interview trick question.
Fun Fact
Brendan Eich created JavaScript in 10 days in May 1995. The var keyword was the only way to declare variables for 20 years until ES6 added let and const in 2015. The name 'JavaScript' was a marketing decision by Netscape to ride Java's popularity — Eich originally called it 'Mocha,' then 'LiveScript,' before management insisted on 'JavaScript.'