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 Using let with for Loops

Nicholas Gaerlan
Nicholas Gaerlan
9,501 Points

Why is there no explanation of hoisting?

I finally figured out the difference between let and var by googling it and finding something about hoisting. I think this is probably the most important idea to explain why you would use let instead of var. Maybe I missed it in the video, but it would be very helpful to edit this vid to show how hoisting works before going into how to use let. I know for me, it made ZERO since without the context of hoisting and even now I'm amazed at how broken scope is/was with JS.

2 Answers

Steven Parker
Steven Parker
231,275 Points

I've never given this much thought, since I consider relying on variable hoisting to be a bad practice. Also note that even with "var" only a declaration is hoisted, not an initial assignment.

So from a practical standpoint in regards to good programming practice, this difference is unimportant. On the other hand, the scope difference between "let" and "var" (as discussed in the course) is most important!

Nicholas Gaerlan
Nicholas Gaerlan
9,501 Points

I had never heard of "hoisting". Before JavaScript, my only exposure to coding was studying Java and C++ 15 years ago. I can't remember if Java has hoisting. I know that in C++ you can pass pointers and achieve something similar, but by default I think I remember that C++ uses block-scope too. My gut tells me "block scope" = good, but "hoisting" = bad. This is all boiler plate discussion I guess. In the end, just use const and let and avoid var.