javascript / beginner
Snippet
Updating Object Properties with Const
Variables declared with const cannot be reassigned to a new value, but the properties of an object stored in a const variable can still be modified.
snippet.js
1
2
3
const user = { name: 'Alex' };user.name = 'Sam';console.log(user.name); // Output: "Sam"
react
Breakdown
1
const user = { name: 'Alex' };
Creates a constant reference to an object.
2
user.name = 'Sam';
Modifies a property inside the object, which is allowed even with const.