var, let, and const
In programming, a variable is a named place to store data so you can use it later.
You can think of a variable like a labeled box:
let userName = "Asha";Here, userName is the label, and "Asha" is the value stored inside it.
JavaScript has three keywords for creating variables:
varletconst
Understanding the difference between them is one of the most important foundations in JavaScript.
Declaring a Variable
To create a variable, you use a declaration keyword, a name, and usually a value.
let message = "Hello";This line means:
- Create a variable using
let. - Name it
message. - Store the string
"Hello"inside it.
You can then use the variable later:
console.log(message);var: The Older Way
var is the original way to declare variables in JavaScript.
You will still see var in older codebases, tutorials, and libraries, but it is rarely the best choice in modern JavaScript.
var age = 25;
var age = 30;
age = 35;
console.log(age); // 35With var:
- You can update the value.
- You can redeclare the same variable name.
- It is function-scoped, not block-scoped.
The redeclaration behavior can make bugs harder to notice:
var total = 100;
var total = 0;
console.log(total); // 0JavaScript does not warn you that total was declared twice. In a larger file, that can be dangerous.
For modern code, avoid var unless you are working in an older codebase that already uses it.
let: For Values That Change
let was introduced in ES6, also known as ECMAScript 2015.
Use let when the value needs to change later.
let score = 0;
score = 10;
score = 20;
console.log(score); // 20With let:
- You can update the value.
- You cannot redeclare the same name in the same scope.
- It is block-scoped.
This is allowed:
let score = 0;
score = 10;This is not allowed:
let score = 0;
let score = 10; // ErrorThat error is helpful because it prevents you from accidentally declaring the same variable twice.
const: For Values That Should Not Be Reassigned
const was also introduced in ES6.
Use const when the variable should not be reassigned.
const pi = 3.14159;
console.log(pi);With const:
- You cannot redeclare the same name in the same scope.
- You cannot reassign it to a new value.
- It is block-scoped.
- You must give it a value immediately.
This is valid:
const country = "India";This is not valid:
const country;
country = "India";This is also not valid:
const country = "India";
country = "Japan"; // Errorconst does not mean the value is deeply frozen. It means the variable name cannot be reassigned.
For example, this is allowed:
const user = {
name: "Asha"
};
user.name = "Ravi";
console.log(user.name); // RaviBut this is not allowed:
const user = {
name: "Asha"
};
user = {
name: "Ravi"
}; // ErrorYou will learn objects and arrays in more detail later. For now, remember this rule: const prevents reassignment of the variable, not every change inside an object or array.
Block Scope
Both let and const are block-scoped.
A block is code inside curly braces {}.
if (true) {
let message = "Inside the block";
console.log(message);
}The variable only exists inside that block.
This will cause an error:
if (true) {
let message = "Inside the block";
}
console.log(message); // ErrorThis is one reason let and const are safer than var. They keep variables limited to the part of the code where they are needed.
Choosing Between let and const
Use const by default.
const appName = "DailyCoder";
const maxLoginAttempts = 5;Use let only when the value needs to change.
let attempts = 0;
attempts = attempts + 1;
attempts = attempts + 1;
console.log(attempts); // 2Avoid var in new code.
Comparison Table
| Keyword | Can be reassigned? | Can be redeclared in same scope? | Scope | Modern usage |
|---|---|---|---|---|
var |
Yes | Yes | Function | Avoid in new code |
let |
Yes | No | Block | Use when the value changes |
const |
No | No | Block | Use by default |
Common Mistakes
Redeclaring a let
let name = "Asha";
let name = "Ravi"; // ErrorUse reassignment instead:
let name = "Asha";
name = "Ravi";Reassigning a const
const language = "JavaScript";
language = "Python"; // ErrorIf the value must change, use let:
let language = "JavaScript";
language = "Python";Using var Out of Habit
var count = 0;Prefer:
let count = 0;Or, if the value will not change:
const count = 0;Best Practice
The modern JavaScript rule is:
- Start with
const. - Change to
letonly if reassignment is needed. - Avoid
varin new code.
This approach makes your code easier to understand and reduces accidental bugs.
Quick Check
Which keyword should you use for a value that will not be reassigned?
const siteName = "DailyCoder";Use const because siteName should keep the same value.
Which keyword should you use for a value that changes?
let score = 0;
score = 10;Use let because score changes over time.