JavaScript Variables Explained: let, const and var for Beginners
Variables are a fundamental part of almost every programming language. In JavaScript, variables allow developers to store information that can be used later in a program.
For example, an application may need to store a user’s name, age, product price, or login status.
JavaScript provides three commonly known variable declarations:
letconstvar
Although all three can be used to create variables, they behave differently.
What Is a Variable?
A variable is a named location used to store a value.
For example:
let username = "Rahul";
In this example, username is the variable and "Rahul" is its value.
The value can then be used elsewhere:
console.log(username);
The output will be:
RahulUsing let in JavaScript
The let keyword is used when you expect the value of a variable to change.
For example:
let score = 10;
score = 20;
console.log(score);
The value starts as 10 and is later changed to 20.
This makes let useful for values that need to be updated during program execution.
For example:
let counter = 0;
counter = counter + 1;
counter = counter + 1;
console.log(counter);
The final value is 2.
Using const in JavaScript
The const keyword is used when a variable should not be reassigned after it has been created.
For example:
const websiteName = "Developers Ground";
You cannot later do this:
websiteName = "Another Website";
JavaScript will generate an error because a const variable cannot be reassigned.
For values that are not expected to change, const is generally a good choice.
Using var in JavaScript
var is the older way of declaring variables in JavaScript.
Example:
var city = "Pune";
console.log(city);
Modern JavaScript code generally prefers let and const because they provide clearer scoping behavior.
You may still encounter var when working with older JavaScript projects or legacy code.
Difference Between let, const and var
The most important differences can be summarized like this:
| Keyword | Can be reassigned? | Scope |
|---|---|---|
let |
Yes | Block scope |
const |
No | Block scope |
var |
Yes | Function scope |
For most modern JavaScript development, you will frequently work with let and const.
What Is Block Scope?
Block scope means that a variable exists only inside the block where it was declared.
For example:
if (true) {
let message = "Hello";
console.log(message);
}
The variable message exists inside the if block.
Trying to access it outside the block will not work:
if (true) {
let message = "Hello";
}
console.log(message);
This produces an error because message is not available outside its block.
Why Is const Commonly Preferred?
Developers often prefer const when they know that a variable does not need to be reassigned.
For example:
const firstName = "Amit";
const age = 25;
const country = "India";
Using const makes your intention clearer.
If you later realize that a value needs to change, you can use let.
For example:
let currentPage = 1;
currentPage = 2;
Can Objects Declared With const Change?
This is an important JavaScript concept.
Consider:
const user = {
name: "Amit",
age: 25
};
You cannot reassign the entire variable:
user = {};
However, you can modify properties inside the object:
user.age = 26;
This works because const prevents reassignment of the variable itself. It does not automatically make the object immutable.
Can Arrays Declared With const Change?
The same idea applies to arrays.
const languages = ["JavaScript", "Java"];
You cannot replace the entire array:
languages = ["Python"];
But you can modify its contents:
languages.push("PHP");
Which One Should Beginners Use?
A simple rule for modern JavaScript is:
Use const by default. Use let when the value needs to change.
Use var mainly when you are working with older code that already uses it.
For example:
const website = "Developers Ground";
let visitors = 100;
visitors = visitors + 10;
Here, website remains unchanged, while visitors can be updated.
Final Thoughts
Understanding variables is an important first step toward learning JavaScript.
Remember these three points:
constis useful when a variable should not be reassigned.letis useful when a value needs to change.varis an older declaration style that you may encounter in existing projects.
Once you understand variables, you can move on to other JavaScript fundamentals such as functions, arrays, objects, conditions, loops, and asynchronous programming.

