Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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.