JavaScript: Difference Between var, let, and const
One of the most foundational JavaScript interview questions is:
What are the differences between
var,let, andconst?
Before ES6 (ES2015), var was the only way to declare variables in JavaScript. ES6 introduced let and const to resolve common pitfalls associated with var, such as scoping bugs and unintentional reassignments.
Interviewers want to see that you understand the three main differences:
- Scope
- Hoisting behavior
- Reassignment and Redeclaration
1. Scope (Where does the variable live?)
var is Function-Scoped
Variables declared with var are scoped to the function they are declared in. If they are declared outside of any function, they become globally scoped and are attached to the window object in browsers.
function testVar() {
if (true) {
var x = 10;
}
console.log(x); // 10 (Accessible outside the if-block!)
}let and const are Block-Scoped
Variables declared with let and const are scoped to the block they are declared in (a block is anything between {}).
function testLet() {
if (true) {
let y = 20;
}
// console.log(y); // ReferenceError: y is not defined
}2. Hoisting (When is the variable accessible?)
All three declarations are "hoisted" to the top of their scope, but they behave differently.
var Hoisting
var is hoisted and initialized with undefined. You can access a var variable before its declaration without throwing an error (though its value will be undefined).
console.log(a); // undefined
var a = 5;let and const Hoisting
let and const are also hoisted, but they are not initialized. They are placed in a "Temporal Dead Zone" (TDZ) from the start of the block until the declaration is evaluated. Accessing them before declaration throws an error.
// console.log(b); // ReferenceError: Cannot access 'b' before initialization
let b = 10;3. Reassignment and Redeclaration
var
Can be reassigned and redeclared within the same scope.
var count = 1;
var count = 2; // Perfectly fine
count = 3; // Also finelet
Can be reassigned, but cannot be redeclared in the same scope.
let score = 10;
// let score = 20; // SyntaxError: Identifier 'score' has already been declared
score = 20; // Perfectly fineconst
Cannot be reassigned and cannot be redeclared. It creates a constant reference.
const API_URL = "https://api.example.com";
// API_URL = "https://new.com"; // TypeError: Assignment to constant variable.Note on const with Objects/Arrays: const prevents reassignment of the variable identifier itself, but it does not make objects or arrays immutable. You can still mutate their properties.
const user = { name: "Alice" };
user.name = "Bob"; // Allowed!Summary Table
| Feature | var | let | const |
|---|---|---|---|
| Scope | Function | Block | Block |
| Hoisting | Yes (initialized to undefined) | Yes (Temporal Dead Zone) | Yes (Temporal Dead Zone) |
| Reassignable | Yes | Yes | No |
| Redeclarable | Yes | No | No |
| Global Object Property | Yes (e.g., window.varName) | No | No |
Senior-Level Interview Answer
The primary differences between
var,let, andconstrevolve around scope, hoisting, and mutability.varis function-scoped and hoisted with an initial value ofundefined, which historically led to confusing bugs.letandconst, introduced in ES6, are block-scoped and hoisted but placed in a Temporal Dead Zone, preventing access before initialization. Furthermore,letallows for reassignment but prevents redeclaration in the same scope, whereasconstestablishes a read-only reference, meaning the identifier cannot be reassigned (though objects assigned toconstcan still have their internal properties mutated). The modern best practice is to default toconstfor immutability and predictability, usingletonly when reassignment is explicitly necessary, and avoidingvarentirely.
Common Interview Mistakes
❌ Believing const makes objects fully immutable
A frequent mistake is stating that const variables cannot be changed at all. Interviewers love to ask: "Can I push to a const array?" The answer is yes. const only locks the binding (the reference), not the underlying data structure. To freeze the object itself, you must use Object.freeze().
❌ Saying let and const are not hoisted
Many candidates mistakenly claim that let and const are not hoisted. They are hoisted, but unlike var, they are not initialized with undefined. They reside in the Temporal Dead Zone until their declaration line is executed.
