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 trialMaksym Novikov
4,580 PointsLoop in listItems
Why the 'var' keyword doesn't work when we replace it in listItems loop? Meanwhile, there's no problems with the 'let'
1 Answer
Steven Parker
231,271 PointsWith "let", the scope is limited to the loop block, so you get a fresh variable each time through the loop. But with "var", the scope is the entire function (or global if not in a function), so there's only one variable that is shared.
Either way is fine while the loop runs, but it makes a difference to any event listener that was set up in the loop. When the event happens later, the value in the "let" variables will still be what it was in the loop, but the value in the "var" will have changed.
Maksym Novikov
4,580 PointsMaksym Novikov
4,580 PointsThank u very much, Steven