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

The question about let tutorial

I just watched the tutorial through the differeces with var, const and let. But the only thing i do not know is for loop section, I still dont know why you click the button, then every button appears 'button 10 pressed'. Hope you guys can help me out, thank you so much!

1 Answer

Steven Parker
Steven Parker
231,248 Points

When you use "var", only one variable for "i" is created, and it keeps being incremented for each button that is created. When the last button is created it is incremented once more to 10 and the loop stops. Then later, when you press any of the buttons, the handler gets the value of "i" which is now 10.

On the other hand, when you use "let", a different variable "i" is created in each loop. So when a button is pressed, the value it gets for "i" is still the same as it was when the button was created.

Thank you so much! So sorry for the late reply, I know the difference between let and var, but I still dont understand it. Can you explain more detailed to me, thank you so much and waiting for your reply.

I watched the tutorial again, the teacher said the i was declared on global scope, but the i lives in the for loop, how can be on the global scope? Is it should be in the local scope?

Steven Parker
Steven Parker
231,248 Points

It has local scope when declared with "let", but global when declared with "var". That's one of the differences in how they work.

For more details, see the MDN pages on var and let.

You help me a lot, but there is still one thing I dont know. The result should less than number 10, but why the result add 1 to the 9?

Steven Parker
Steven Parker
231,248 Points

The loop repeats while "i" is less than buttons.length (which is 10). So the last time the loop code runs is while "i" is 9, and then it is incremented to 10, which is what stops the loop. But it leaves the value of 10 in "i".

Thank you so much! But still dont understand, the result should be 0 to 9, why the 9 increment to 10?

Steven Parker
Steven Parker
231,248 Points

You're quite right that the loop code runs for values of 0 to 9. But after the last pass, the 9 is incremented to 10, and only then does it fail the loop's conditional test and cause the loop to stop running.

It's typical for a loop variable to end with a value one higher than the last pass. But what's unusual is for the variable to be used again at that point. So normally that last value is never seen.

Thank you so much! I'm know a lot, but still there is few things I not know, I just going to keep learning. So, if i meet the loop problem i just change the var to the let. If I have a question again, i will reach you again. Thank you!