Modifying constants

suggest change

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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Declarations and assignments:
* Syntax
* Modifying constants

Table Of Contents
5 Declarations and assignments
11 Arrays
12 Objects
14 Classes
16 Map
17 Set
24 Loops
27 Date
29 Scope
30 AJAX
35 Cookies
41 JSON
44 Fetch
45 Modules
46 Screen
64 Console
68 Symbols
73 Modals
76 Events
86 Proxy
89 WeakMap
90 WeakSet
102 Tilde