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 Modify Object Properties

Why aren't you using let to define variables inside function?

.

5 Answers

Steven Parker
Steven Parker
231,261 Points

The function is only accessing variables that have already been declared and are then passed to it as arguments. So these are not local variables of the function and trying to declare them again would cause an error.

.

Steven Parker
Steven Parker
231,261 Points

I meant declared as you would using "let" or "var" or "const". You use those to create local variables, but you don't use them with the parameters (as "prod" is in this case).

.

.

Steven Parker
Steven Parker
231,261 Points

Not only could, but declaring new variables explicitly is a recommended "best practice".

a follow up question i have is this. why he declare the product object with let instead of const?, it's because the inventory property will change?

should i always declare my variables with let first? and then if the requirement is met and the variable wont change anymore its a const?

Steven Parker
Steven Parker
231,261 Points

If later code might need to change the value, "let" is a good choice. Use "const' when you are reasonably certain (because of what is represents or how is used) that the value won't be changed.