let vs const vs var in JavaScript: What Is the Difference?
Learn the difference between let, const, and var in JavaScript with simple examples. Understand scope, hoisting, redeclaration, reassignment, and when to use each.

let vs const vs var in JavaScript: What Is the Difference?
If you are learning JavaScript or preparing for a frontend interview, you will almost certainly come across these three keywords:
var, let, and const.
All three are used to create variables, but they do not behave in exactly the same way. The differences become especially important when you work with scope, hoisting, reassignment, and redeclaration.
In this article, we will understand let, const, and var with simple examples.
What are var, let, and const?
JavaScript provides three keywords for declaring variables:
var name = "Vivek";
let age = 25;
const country = "India";All three create variables, but they have different rules. The main differences are:
Feature | var | let | const |
Scope | Function | Block | Block |
Can be reassigned? | Yes | Yes | No |
Can be redeclared in the same scope? | Yes | No | No |
Hoisted? | Yes | Yes | Yes |
Can be used before declaration? | undefined | No | No |
Introduced | Older JavaScript | ES6 | ES6 |
Let's understand each difference.
1. var vs let vs const: Scope
Scope means where a variable can be accessed in your code. This is one of the most important differences.
var has function scope
function test() {
var name = "Vivek";
if (true) {
var age = 25;
}
console.log(age);
}
test();This works because var does not create a separate scope for the if block. The "age" variable belongs to the function.
let and const have block scope
function test() {
if (true) {
let age = 25;
const name = "Vivek";
console.log(age);
console.log(name);
}
console.log(age); // Error
console.log(name); // Error
}
test();Here, let and const are available only inside the { } block where they were declared. This is called block scope.
Simple way to remember
- var: function scope
- let: block scope
- const: block scope
2. Can We Reassign the Variable?
Reassignment means giving a new value to an existing variable.
var can be reassigned
var age = 25;
age = 30;
console.log(age);Output:
30let can also be reassigned
let age = 25;
age = 30;
console.log(age);Output:
30const cannot be reassigned
const age = 25;
age = 30;This gives an error.
TypeError: Assignment to constant variableSo:
- var: can be reassigned
- let: can be reassigned
- const: cannot be reassigned
3. Can We Redeclare a Variable?
Redeclaration means creating another variable with the same name in the same scope.
var allows redeclaration
var name = "Vivek";
var name = "Rahul";
console.log(name);Output:
RahulJavaScript allows this with the var keyword.
let does not allow redeclaration
let name = "Vivek";
let name = "Rahul";This causes an error.
const also does not allow redeclaration
const name = "Vivek";
const name = "Rahul";This also causes an error.
So:
- var: redeclaration allowed
- let: redeclaration not allowed
- const: redeclaration not allowed
This is one reason modern JavaScript code usually prefers let and const.
4. What Is Hoisting?
Hoisting is another common JavaScript interview question. JavaScript processes declarations before executing the code in that scope.
However, var, let, and const behave differently when you try to access them before their declaration.
var and hoisting
console.log(name);
var name = "Vivek";Output:
undefinedIt is roughly understood as:
var name;
console.log(name);
name = "Vivek";The declaration is available before the assignment.
let and const
Now look at this:
console.log(name);
let name = "Vivek";This gives an error.
The same happens with const:
console.log(name);
const name = "Vivek";You cannot access them before their declaration.
This is related to something called the Temporal Dead Zone, commonly called the TDZ.
5. What Is the Temporal Dead Zone?
The Temporal Dead Zone is the period between entering a scope and the point where a let or const variable is declared.
For example:
console.log(name);
let name = "Vivek";
or
let name;
↓ this period
name = "Rohan";The name variable exists in the scope, but you cannot access it before the declaration is reached. That period is called the Temporal Dead Zone.
A simple way to remember it:
var
↓
Can access before declaration
↓
undefined
let / const
↓
Cannot access before declaration
↓
ReferenceError6. Is const Really Immutable?
This is a tricky question. Many developers think const means the value can never change. That is not always true.
Consider an object:
const user = {
name: "Vivek"
};
user.name = "Rahul";
console.log(user.name);Output:
RahulThis works. Why? Because const prevents reassignment of the variable itself. It does not make the object completely immutable.
This will fail:
const user = {
name: "Vivek"
};
user = {
name: "Rahul"
};The variable user cannot point to a completely different object. But the properties inside the object can still be changed. The same idea applies to arrays:
const numbers = [1, 2, 3];
numbers.push(4);
console.log(numbers);Output:
[1, 2, 3, 4]But this is not allowed:
numbers = [5, 6, 7];So remember: const prevents reassignment, not all changes inside objects and arrays.
7. var vs let Inside a Loop
This is one of the most useful examples to understand the difference. Consider:
for (var i = 0; i < 3; i++) {
console.log(i);
}
console.log(i);Output:
0
1
2
3The i variable is still available outside the loop because var is function-scoped. Now use let:
for (let i = 0; i < 3; i++) {
console.log(i);
}
console.log(i);The last line causes an error because i exists only inside the loop block.
This is one of the practical reasons let is safer than var.
8. Which One Should You Use?
For modern JavaScript development, a simple rule works well:
Use const by default
If you don't need to reassign the variable:
const name = "Vivek";
const age = 25;Use let when the value needs to change
let count = 0;
count++;
console.log(count);Avoid var in new code
var is still valid JavaScript, and you will see it in older projects.
But for new JavaScript applications, let and const are generally preferred because their block scope and redeclaration rules make code easier to understand and avoid some common mistakes.
let vs const vs var: Easy Interview Answer
If an interviewer asks:
"What is the difference between var, let and const?"
You can answer: "var is function-scoped and allows both reassignment and redeclaration. let is block-scoped and allows reassignment but not redeclaration in the same scope. const is also block-scoped, but it does not allow reassignment or redeclaration. var can be accessed before its declaration and gives undefined, while let and const are in the Temporal Dead Zone until their declaration is reached".
That is a strong answer for a frontend interview.
Quick Example
var a = 10;
let b = 20;
const c = 30;
a = 15; // Allowed
b = 25; // Allowed
c = 35; // ErrorAnd:
var x = 10;
var x = 20; // AllowedBut:
let y = 10;
let y = 20; // ErrorAnd:
const z = 10;
const z = 20; // ErrorSummary
You don't need to memorize a long list of rules. Just remember these three points:
var
- Function scoped
- Can be reassigned
- Can be redeclared
- Hoisted and initialized with undefined
let
- Block scoped
- Can be reassigned
- Cannot be redeclared in the same scope
- Cannot be accessed before declaration
const
- Block scoped
- Cannot be reassigned
- Cannot be redeclared in the same scope
- Cannot be accessed before declaration
For modern JavaScript, a good rule is: Use const by default. Use let when the value needs to change. Use var mainly when working with older JavaScript code.
Once you understand these differences, many other JavaScript concepts such as scope, closures, hoisting, and the Temporal Dead Zone become much easier to understand.
Frequently Asked Questions
Should I use let or const in JavaScript?
Use const when you do not need to change the value. Use let when the value needs to be changed later. For most modern JavaScript code, it is a good practice to use const by default and use let when needed.
Why should I avoid var in modern JavaScript?
var has function scope and allows redeclaration, which can sometimes cause unexpected results. let and const have block scope and provide safer rules, so they are generally preferred in modern JavaScript development.
Can a const object or array be changed?
Yes. const prevents you from assigning a new value to the variable, but it does not make an object or array completely unchangeable. You can still change its properties or add and remove items from an array.
Version History 2 updates
- Sep 13, 2026
Updated: title, description, content, categories, tags, featuredImage, images, status, faqs, seriesName, seriesOrder, wordCount, readingTimeMinutes, slug, rejectionReason
- Sep 13, 2026
Updated: title, description, content, categories, tags, featuredImage, images, status, faqs, seriesName, seriesOrder, wordCount, readingTimeMinutes, slug, rejectionReason