Difference Between var, let and const in JavaScript
The difference between var, let and const in JavaScript: scope, hoisting, reassignment and the temporal dead zone, with examples and interview tips.
Expected Interview Answer
var is function-scoped and hoisted with an undefined initial value, while let and const are block-scoped and stay in a temporal dead zone until declared; let can be reassigned, but const cannot be reassigned after its initial binding.
var declarations are hoisted to the top of their function and can be redeclared, which often leads to accidental bugs. let and const, introduced in ES6, are limited to the nearest curly-brace block and throw a ReferenceError if accessed before their declaration line. const prevents reassignment of the binding, though the contents of a const object or array can still be mutated. Modern JavaScript favors const by default and let when reassignment is needed, avoiding var.
- Block scoping with let and const prevents leaking variables
- const signals intent that a binding will not change
- The temporal dead zone catches use-before-declaration bugs
- Avoiding var reduces accidental redeclaration errors
- Cleaner, more predictable variable behavior in loops and closures
AI Mentor Explanation
var, let and const are like three ways of assigning a batting position. var is a loose verbal agreement anyone can override, let is a chalked-in slot that can be rubbed out and changed before play, and const is the name printed on the official team sheet that cannot be swapped once submitted.
Step-by-Step Explanation
Step 1
Choose the scope
var is function-scoped; let and const are limited to the nearest block, such as an if or for body.
Step 2
Understand hoisting
All three are hoisted, but var is initialized to undefined while let and const stay uninitialized.
Step 3
Mind the temporal dead zone
Accessing a let or const before its declaration line throws a ReferenceError.
Step 4
Decide on reassignment
Use let when the value will change and const when the binding should stay fixed.
Step 5
Prefer const by default
Reach for const first, switch to let only when reassignment is required, and avoid var.
What Interviewer Expects
- Clear distinction between function scope and block scope
- Knowledge that const prevents reassignment but not object mutation
- Understanding of the temporal dead zone for let and const
- Awareness that var can be redeclared while let and const cannot
- A recommendation to prefer const and let over var
Common Mistakes
- Claiming const makes objects fully immutable
- Saying let and const are not hoisted at all
- Thinking var is block-scoped like let
- Confusing reassignment with redeclaration
- Using var in modern code out of habit
Best Answer (HR Friendly)
“var, let and const are three ways to declare variables in JavaScript. var is the old style with looser rules, while let and const are the modern choices — let is for values that change and const is for values that should stay the same. Today most developers use const by default and let when needed.”
Code Example
function demo() {
if (true) {
var a = 1; // function-scoped
let b = 2; // block-scoped
const c = 3; // block-scoped, cannot be reassigned
}
console.log(a); // 1 (var leaks out of the block)
// console.log(b); // ReferenceError: b is not defined
}
let count = 0;
count = 1; // OK: let can be reassigned
const user = { name: 'Ada' };
user.name = 'Grace'; // OK: mutating the object is allowed
// user = {}; // TypeError: cannot reassign a const bindingFollow-up Questions
- What is the temporal dead zone?
- Can you mutate an object declared with const?
- Why does var leak out of blocks like if and for?
- What problems did let and const solve compared to var?
- How do var and let behave differently inside a for loop with closures?
MCQ Practice
1. Which keyword creates a block-scoped variable that can be reassigned?
let is block-scoped and allows reassignment, unlike const which is block-scoped but cannot be reassigned.
2. What happens if you access a let variable before its declaration?
let and const live in the temporal dead zone until their declaration line, so accessing them earlier throws a ReferenceError.
3. Which statement about const is true?
const prevents reassigning the binding, but the contents of a const object or array can still be mutated.
Flash Cards
Scope of var vs let/const? — var is function-scoped; let and const are block-scoped to the nearest curly braces.
Can const values be mutated? — The binding cannot be reassigned, but object or array contents held by a const can still be changed.
What is the temporal dead zone? — The span between entering a scope and a let/const declaration where accessing the variable throws a ReferenceError.
Which should you default to? — Use const by default, let when reassignment is needed, and avoid var in modern code.