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
sulaiman abouabdah
5,314 PointsWhy use const at all for arrays and objects
Why using const if it doesn't prevent a lot of things. It adds nothing but confusion. It makes sense to use let or var instead. It doesn't make sense that you can still update the first_name if you use person.first_name = "NewName" but it doesn't work if you do person = {first_name: "NewName"} because the first one still does the job which shouldn't happen
https://teamtreehouse.com/library/using-constants-with-arrays-and-objects-2
1 Answer
Steven Parker
243,199 PointsA variable is a reference to an object, it's not the object itself. So if you declare it "const", that guarantees it will always refer to that same object. But as you discovered, it does not mean that the object itself cannot be modified.
An object that cannot be modified at all is considered "frozen", but It's very rare for this to be needed.
sulaiman abouabdah
5,314 Pointssulaiman abouabdah
5,314 PointsSorry, but you didn't answer my question. So why use it with arrays and objects?
Milan Toth
12,182 PointsMilan Toth
12,182 PointsIt does make sense. You can change the properties and methods of the object with const, but cannot accidentally reassign the whole object itself for example.
Steven Parker
243,199 PointsSteven Parker
243,199 PointsMaking the variable "const" means that it will always refer to the array or object it was originally initialized with.