I have been working on some exercise with let
and const
and found something interesting with const
along with objects assignments.
As per definition: In JavaScript,
const
means that the identifier can’t be reassigned.
const numConst = 20;
numConst = 40;
console.log("numConst", numConst);
Uncaught TypeError: Assignment to constant variable.
Whereas when I try to modify the object declared with const, it is allowed:
const person = { name: "test", age: 20 };
person.age = 40;
console.log("person.age", person.age); // outputs: person.age 40
Why does const
behave differently with object?