var vs let vs const in JavaScript?
Understand the differences between var, let, and const in JavaScript, covering scope, hoisting, reassignment rules, and practical code examples.
Expected Interview Answer
var, let, and const are the three ways to declare variables in JavaScript, differing mainly in scope, reassignment rules, and hoisting behavior. var is function-scoped and can be redeclared, let is block-scoped and reassignable, and const is block-scoped but cannot be reassigned after initialization.
var was the original declaration keyword and is scoped to the nearest enclosing function, ignoring block boundaries like if statements or loops, which frequently causes leaked variables. let and const, introduced in ES6, are block-scoped, meaning they only exist within the nearest pair of curly braces. var is hoisted and initialized to undefined, while let and const are hoisted into the temporal dead zone and throw if accessed early. const requires an initializer and forbids reassignment of the binding, though objects and arrays assigned to a const variable can still have their contents mutated.
- Block scoping with let/const avoids accidental variable leakage
- const signals intent and prevents accidental reassignment
- Reduces bugs from var's function-scope and redeclaration quirks
- Clearer code readability about mutability intent
- Modern JS style guides default to const, then let
AI Mentor Explanation
var is like a substitute fielder allowed to roam and cover any position across the whole innings, regardless of which fielding block was set. let is a specialist fielder assigned strictly to one designated zone for that particular over, unable to wander outside it. const is like the fixed boundary rope itself — once the ground is marked, that line does not move for the rest of the match.
Step-by-Step Explanation
Step 1
Scope difference
var is function-scoped; let and const are block-scoped, confined to the nearest curly braces.
Step 2
Redeclaration
var allows redeclaring the same name in the same scope; let and const throw a SyntaxError on redeclaration.
Step 3
Hoisting behavior
var is hoisted to undefined; let/const are hoisted into the temporal dead zone.
Step 4
Reassignment
var and let can be reassigned; const cannot be reassigned after its initial binding.
Step 5
Mutability of const objects
const only locks the binding, not the value — properties of a const object or array can still be mutated.
What Interviewer Expects
- Explains function scope vs block scope clearly
- Knows const prevents reassignment, not mutation
- Understands the temporal dead zone applies to let/const
- Can give an example of var leaking out of a block
- Recommends const by default, let when reassignment is needed
Common Mistakes
- Saying const makes objects fully immutable
- Believing var and let behave identically except for the keyword
- Not knowing var can be redeclared without error
- Forgetting that let/const are still hoisted, just not initialized
- Using var out of habit in modern codebases
Best Answer (HR Friendly)
“var, let, and const are all ways to create variables in JavaScript, but they differ in where they are accessible and whether they can be changed. Modern JavaScript mostly uses const for values that don't change and let for ones that do, avoiding var due to its confusing scoping rules.”
Code Example
function demo() {
if (true) {
var x = 1;
let y = 2;
}
console.log(x); // 1 (leaks out of block)
try {
console.log(y); // ReferenceError: y is not defined
} catch (e) {
console.log(e.message);
}
}
demo();
const obj = { name: 'SkillVeris' };
obj.name = 'Updated'; // allowed, mutating contents
console.log(obj.name); // Updated
try {
obj = {}; // TypeError: Assignment to constant variable.
} catch (e) {
console.log(e.message);
}Follow-up Questions
- Why does var leak outside of if blocks and loops?
- Can you redeclare a let variable in the same scope?
- Is a const array or object truly immutable?
- How does block scoping affect closures inside loops?
- When would you still choose let over const?
MCQ Practice
1. Which keyword is function-scoped rather than block-scoped?
var ignores block boundaries and is scoped to the nearest enclosing function.
2. What happens if you reassign a const variable?
const bindings cannot be reassigned; attempting to do so throws a TypeError at runtime.
3. Can properties of a const-declared object be changed?
const prevents reassigning the variable binding, but the object's internal properties remain mutable unless frozen explicitly.
Flash Cards
Scope of var? — Function-scoped — ignores block boundaries like if and for.
Scope of let and const? — Block-scoped — confined to the nearest enclosing curly braces.
Can const be reassigned? — No, reassigning a const binding throws a TypeError.
Is a const object immutable? — No, only the binding is locked; object properties can still be mutated.