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 trialFarhad Lipo
51 PointsCan you mention what is exactly the scope of 'let' and 'var' in the video for loop?
Guys,
Just believe me I know the difference between 'let' and 'var' scopes. I just can't understand why using 'let' can solve the problem? what's the problem with using 'var'? What the exact scopes for var and let in the video for loop? Can you explain step by step what's happening in the behind of scenes in the both forms? I need your help, devs.
Thanks so much
4 Answers
Steven Parker
231,275 PointsThe scope of "var" is global, so all the handlers share the same "i", which is left after the loop with a value one higher than the last time through the loop.
But the scope of "let" is just inside the loop, so each handler gets a separate "i" which retains the value it had when the handler was established.
Ari Misha
19,323 PointsHiya there! There is a worskshop video in Treehouse that explains everything about closures and why "let" was introduced to JS. Check out the video here.
Diana Ci
18,672 PointsOne more explanation: https://www.youtube.com/watch?v=oeGaXKU_MYw Hope it helps :)
S G
6,243 PointsReally good and to the point explanation in this video, thank you
Diana Ci
18,672 PointsAnother good explanation here on teamtreehouse: https://teamtreehouse.com/library/using-let-with-for-loops-2
Farhad Lipo
51 PointsFarhad Lipo
51 PointsThanks, Let me know if i catch it correctly. I love stories and try to make tow:
VAR : "When we use 'var' in for loop we loop around given statements 10 times and actually give every button a handler which not have been fired yet. Those handlers have a variable which refer to 'i' value. Now when user clicks and fires a handler behind the scene handlers look at the 'i' value and it finds number 10. "
LET : ( Oh, to be honest. I can't say the story when we use 'let' keyword. Because it's all about local. copy and such not-clear topics and words. Steven can you help me make the second brief story in 3 lines like above?
Steven Parker
231,275 PointsSteven Parker
231,275 PointsHow's this: "When we use 'let' in this for loop, we loop around the given statements 10 times and actually give every button a handler which has not been fired yet. Those handlers have variables named "i" which are all different. Now when user clicks and fires a handler behind the scene handlers look at the 'i' value and find the same value it had when the loop was running. "