Can You Change a const Value in JavaScript?
Learn whether you can change a const value in JavaScript. Understand primitive vs reference types, objects, arrays, and why const doesn't always mean immutable.
Muhammad Ali
Full Stack Developer

Learn whether you can change a const value in JavaScript. Understand primitive vs reference types, objects, arrays, and why const doesn't always mean immutable.
One of the most common JavaScript interview questions is:
Can we change a
constvalue?
Most developers immediately answer:
No.
But that's only partially correct.
The real answer is:
It depends on what the
constis storing.
Let's understand why.
What Does const Actually Mean?
Many developers think const means the value can never change.
That's not true.
const simply means the variable cannot be reassigned to a new value.
Whether the value itself can change depends on its type.
Example 1: Primitive Values ❌
Primitive values cannot be changed.
const age = 24;
age = 25;
Output:
TypeError: Assignment to constant variable.
Because you're trying to assign a completely new value.
The same applies to:
- Number
- String
- Boolean
- Null
- Undefined
- Symbol
- BigInt
Example 2: Objects ✅
Now let's look at an object.
const user = {
name: "TechCamp Pro",
age: 24,
};
user.age = 25;
console.log(user);
Output
{
name: "TechCamp Pro",
age: 25
}
Did JavaScript allow this?
Yes.
Because you didn't replace the object.
You only changed one of its properties.
Example 3: Arrays ✅
The same rule applies to arrays.
const fruits = ["Apple", "Mango"];
fruits.push("Orange");
console.log(fruits);
Output
["Apple", "Mango", "Orange"]
This works perfectly.
Because the array reference never changed.
What Doesn't Work?
Replacing the entire object.
const user = {
name: "Ali",
};
user = {
name: "Ahmed",
};
Output
TypeError: Assignment to constant variable.
JavaScript prevents reassignment.
Why Does This Happen?
Objects and arrays are reference types.
When you write:
const user = {
name: "Ali",
};
The variable stores a reference (memory address) to the object.
user
│
▼
Memory
{
name: "Ali"
}
Changing a property modifies the existing object in memory.
user.name = "Ahmed";
The reference stays the same.
But this creates a new object:
user = {
name: "Ahmed",
};
Now the reference changes, which const does not allow.
If You Want a Truly Immutable Object
Use Object.freeze().
const user = Object.freeze({
name: "Ali",
});
user.name = "Ahmed";
console.log(user.name);
Output
Ali
The object cannot be modified.
Note:
Object.freeze()is shallow. Nested objects can still be changed unless they are frozen as well.
Interview Tip
If an interviewer asks:
Can we change a
constvalue?
A strong answer is:
Primitive values cannot be reassigned. Objects and arrays declared with
constcan have their contents modified becauseconstprotects the reference, not the object itself.
Key Takeaways
constprevents reassignment.- Primitive values cannot be changed.
- Object properties can be updated.
- Array elements can be added or removed.
- Replacing the entire object or array throws an error.
- Use
Object.freeze()for immutability.
Conclusion
The biggest misconception about const is that it makes values immutable.
In reality, const only prevents changing the variable's reference.
If the variable holds an object or array, you can still modify its contents—as long as you don't assign a completely new object or array.
Understanding this difference is essential for writing better JavaScript and performing well in technical interviews.
Frequently Asked Questions
No. It only prevents reassignment. Object properties can still change.
Get new posts in your inbox
Occasional notes on code, craft, and things I break along the way. No spam — unsubscribe anytime.
Written by
Muhammad Ali
Full Stack Developer
