JavaScript Declarations and Assignments Modifying constants

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

Declaring a variable const only prevents its value from being replaced by a new value. const does not put any restrictions on the internal state of an object. The following example shows that a value of a property of a const object can be changed, and even new properties can be added, because the object that is assigned to person is modified, but not replaced.

const person = { 
    name: "John" 
};
console.log('The name of the person is', person.name);

person.name = "Steve";
console.log('The name of the person is', person.name);

person.surname = "Fox";
console.log('The name of the person is', person.name, 'and the surname is', person.surname);

Result:

The name of the person is John
The name of the person is Steve
The name of the person is Steve and the surname is Fox

In this example we've created constant object called person and we've reassigned person.name property and created new person.surname property.



Got any JavaScript Question?