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 trialNatalie Forster
1,710 PointsSo, when using a const vs a let variable, I thought const was not able to be changed or was immutable? We can add to it?
In other words, it looks here like we are able to add to the "box" of data that is held within the const variable type but cannot change or remove what is already held in it? Does that also mean that when adding new information to the message variable, that the new information then becomes immutable? Would love to understand more!
2 Answers
Natalie Forster
1,710 PointsThat is definitely helpful, thank you!
Adam N
70,280 PointsAn easier way to think about it is that const prevents reassignment. So, we can't change a const variable to a different array , but we can add things to the array that's already assigned to it. Example:
const arr = [1, 2, 3]
// TypeError: Assignment to constant variable.
arr = [4, 5, 6]
// adding is all good
arr.push(7)
Let me know if this helps!
Adam N
70,280 PointsAdam N
70,280 Pointsnp! You can mark this question as solved by selecting a "best answer".