Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

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.