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 REST APIs with Express Create, Read, Update, Delete Edit A Quote

When updating quotes, why declared variable using "const" rather than "var/let"?

When updating quotes, why declared variable using "const" rather than "var/let"? const means the variable declared cannot be modified, doesn't it? (But we want to update the quotes. )

2 Answers

Brandon White
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brandon White
Full Stack JavaScript Techdegree Graduate 35,771 Points

Hi Tim,

This is a good question. Hopefully, the following makes at least a little sense...

JavaScript is a high level programming language (as it removes a lot of the abstraction that comes with writing code, handling memory, etc...). Because an object is not a primitive type, when you declare a variable and assign it an object, the variable is assigned a pointer. The pointer is like an address that "points" to where that object is in your memory.

So while const does essentially say "don't change the value of this variable", when you're updating an object in the way Treasure does in the video, she's not changing the value of quotes. The quotes variable will still point to the same place in memory where the object is stored, and that place in memory is where the data is being updated.

It's the same reason that you can declare an array with the const keyword, and still push() items to it.

If you want to update the quotes, then I would use var or let. Using the const variable will throw an error.