JavaScript Getting Started With ES2015 Defining Variables With let and const Using Constants with Arrays and Objects

Why dot notation let first_name change?

In the video, when Andrew used [] to reassign to value of first_name, it throws a error but when he did it with dot notation it let him change it. why the difference?

2 Answers

I see what you're referring to. At the end of the video, he tried to assign a first_name property to person by creating another object and that is what caused the error. Right after that, he simply took the existing object and updated the first_name property to it, which is allowed.

You can update and add properties to an existing object created with const, but you can't create an entirely new object with it.

Hope that makes sense.

Gamzat Mukailov
Gamzat Mukailov
12,627 Points

const just saving the pointer to the object so you cant assign a new object

example const person = {firstname : "name"}; person is now a pointer to the object {firstname : "name"} you can modify that object as usual person.lastName = "lastname";

but if you try to say, that the pointer person should adress an other object it will call an error person = {newObject : "value"};

with the const attribute you make sure, that the pointer only adress tho this one object.