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 JavaScript Functions Create Reusable Code with Functions Create and Call a Function

monique champagne
monique champagne
761 Points

Confused about using const here..

read the other users questions about this same thing but still not sure i understand why const can be used here even after reading these responses.. what is constant here if the number keeps changing?

is it like- declaring a variable with const is like creating a box that you can put things in and out of.. the box is- lets say, called const myBox & the things we put inside of it are toys.. so

const myBox = ["hula hoop", "puzzle", "checkers"]; so you can add more toys to the box (like using the push() method), but you cant- do what?? thats the part im getting lost at- what makes this different than using let?

is it so that somewhere down the line you dont use myBox again for some other random thing?

confused...

2 Answers

Steven Parker
Steven Parker
229,759 Points

What's "constant" is the variable itself, not the object it refers to. So for example:

const myBox = ["hula hoop", "puzzle", "checkers"];
myBox.push("chess");  // adding an item to the array is allowed
myBox = 42;           // but reassigning the variable is not!
Steven Parker
Steven Parker
229,759 Points

Modifying the contents isn't a "step" to reassigning. If it's a normal (not const) variable, you can reassign it at any time.

So if you have a const array, you can change what's in the array, but it will always be an array. You can't re-use it to hold a number or a string, for example.

monique champagne
monique champagne
761 Points

so you CAN change the contents of the box totally -

ie- using splice you can get rid of all current toys and put all new toys in seemingly "changing" the contents entirely so that it is nothing like the original -

but you cant do this (change the contents) without actually modifying the original contents...

you also cant just "reassign" it all together... skipping the steps of modifying the original contents.. ?