Can we change value of const object in JavaScript?

Forums JavaScriptCan we change value of const object in JavaScript?
Staff asked 4 years ago

Answers (1)

Add Answer
Shaikh Hafeezjaha Marked As Accepted
Staff answered 3 years ago

A value of the const variable cannot be reassigned.

const val = 10; // 10
val = 15; // Uncaught TypeError: Assignment to constant variable

But you can change the properties of a constant object.

const car = {type:"Fiat", model:"500", color:"white"};
// Change a property:
car.color = "red";

You cannot assign the whole object to a const variable

const car = {type:"Fiat", model:"500", color:"white"};

const car2 = {type:"Test", model:"200", color:"Red"};

car = car2; //Uncaught TypeError: Assignment to constant variable

Subscribe

Select Categories