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 trialAdam Tyler
5,607 Pointsconst of let for object?
In the video we see that the name can be changed of the object, even though it was declared as a const.
I thought const meant that we are unable to change it?
This is how it works for variables, as if I set: const y = 5; y += 1;
I would get : 'Uncaught TypeError: Assignment to constant variable.'
So why can we change the values in the object if it was declared as const?
2 Answers
Gabriel Plackey
11,064 PointsThe variables name is not changing, a value inside of the object is changing. The "name" inside of the Object "student" can be changed, or any value inside of the object can be changed, added, or removed.
The const variable in this case is not being changed, students will always be an object. Unlike your example of const y = 5; y += 1; you are changing the variable "y" from 5 to 6 so "y" is not always 5 therefore let would be better used.
You can test soemthing like this yourself with something like
const testVar = {};
testVar.name = 'myName';
console.log(testVar);
testVar.name = 'newName';
console.log(testVar);
Gabriel Plackey
11,064 PointsConst is constant, or a constant variable that will not change in it's meaning or value. This does not mean it cannot be mutated though in the instance of objects or arrays. Let allows you to change the variables meaning or value itself.
The variable testVar is not changing it's meaning from being an object, we are mutating an item or property inside of it. It is still "constant" in it's meaning, it is an object. If you define y as 5 then want to change it to 6, it is no longer "constant" in it's meaning of being 5, so a const is not usable.
Specifically for an object, let would allow you to redefine the variable testVar. Using a const though means it will always be an object, specifically this object, but we can still change or mutated whatever properties the object holds by changing, adding, or removing. "name" is a property inside of the object, not the value of the variable.
Tony Shangkuan
7,200 PointsOkay, following your explanation, I still dont get the idea of declaring an object using "let" in this case, specifically asking if there is any practical scenario that we would want to avoid declaring an object using "let"?
Adam Tyler
5,607 PointsAdam Tyler
5,607 PointsHi. Thanks, for your comment, but I am still a bit confused.
I don't see how it is different for y to change from 5, to 6 vs an object changing from testVar.name = 'myName' to testVar.name = 'newName'?
I guess I'm really asking, what is the difference between an object that has been defined with 'let' and one defined with ;const'?
Thanks