Sometimes, we need to store a value that should not be changed later in the program. For example, the value of π, a website name, or a fixed configuration value.
For this, JavaScript provides the const keyword.
What is a Constant?
A constant is a variable whose binding cannot be reassigned after it has been declared.
In JavaScript, we create constants using the const keyword:
const pi = 3.14159;Here, pi stores a value that we don't want to reassign.
We can use it just like a normal variable:
const pi = 3.14159;
console.log(pi);Output:
3.14159Why Use const?
Using const tells other developers—and yourself—that the variable should not be reassigned.
For example:
const websiteName = "CS Learning Portal";If there is no reason for websiteName to change, const is a better choice than let.
It makes your code easier to understand and helps prevent accidental reassignment.
Trying to Change a Constant
Once a const variable has been assigned a value, we cannot assign another value to it.
const age = 20;
age = 25;JavaScript will produce an error because age cannot be reassigned.
So this is valid:
const age = 20;
console.log(age);But this is not:
const age = 20;
age = 25; // ❌ ErrorA const Must Be Initialized
Unlike let, a const variable must be given a value when it is declared.
This is valid:
const country = "India";But this is invalid:
const country; // ❌ ErrorYou cannot declare a const variable first and assign its value later.
const with Different Data Types
A constant can store different types of values.
const name = "John";
const age = 21;
const price = 99.99;
const isStudent = true;So const does not mean that the value must be a number or string. It simply means the variable cannot be reassigned.
Can Objects and Arrays Change?
This is an important point for beginners.
const prevents reassignment, but it does not make objects or arrays completely immutable.
For example:
const fruits = ["Apple", "Banana"];
fruits.push("Mango");
console.log(fruits);Output:
["Apple", "Banana", "Mango"]This works because we didn't replace the fruits array. We modified the contents of the existing array.
But this is not allowed:
const fruits = ["Apple", "Banana"];
fruits = ["Mango", "Orange"]; // ❌ ErrorSo remember:
constprevents reassignment of the variable, not necessarily modification of the object or array it refers to.
You don't need to worry too much about this distinction yet—we'll understand it better when we learn arrays and objects.
const vs let
The main difference is whether the variable can be reassigned.
Feature |
|
|
|---|---|---|
Can be declared | ✅ | ✅ |
Must be initialized immediately | ❌ | ✅ |
Can be reassigned | ✅ | ❌ |
Modern JavaScript | ✅ | ✅ |
For example:
let score = 50;
score = 80; // ✅ Allowed
const pi = 3.14;
pi = 3.14159; // ❌ Not allowedWhen Should You Use const?
A good modern JavaScript practice is:
Use
constby default. Useletonly when the value needs to change.
For example:
const name = "John";
const country = "India";
let score = 50;
score = 75;Here, name and country don't need to change, so we use const.
The score can change, so we use let.
Conclusion
The const keyword is used when you don't want a variable to be reassigned after its initial value is set.
The basic syntax is:
const variableName = value;The most important things to remember are:
constvariables must be initialized when declared.They cannot be reassigned.
constis commonly preferred when a value doesn't need to change.constdoes not automatically make arrays and objects immutable.
Once you understand const, you have the basic idea of how JavaScript handles values that should stay assigned to the same variable.