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 trialvamosrope14
14,932 PointsWhy aren't you using let to define variables inside function?
.
5 Answers
Steven Parker
231,261 PointsThe 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.
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 PointsI'm not seeing any variables defined inside functions in this video, just existing variables accessed, such as prod.inventory -= quantity
.
vamosrope14
14,932 Points.
vamosrope14
14,932 Points.
Steven Parker
231,261 PointsNot only could, but declaring new variables explicitly is a recommended "best practice".
<noob />
17,062 Pointsa 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
231,261 PointsIf 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.
vamosrope14
14,932 Pointsvamosrope14
14,932 Points.
Steven Parker
231,261 PointsSteven Parker
231,261 PointsI 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).