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
Tani Huang
6,952 PointsQuestion of Const Let Var : alert ("Button" + i + "Pressed");
Know there have many answers of this question, but I still confuse...here is the script
<button>0</button>
<button>1</button>
<button>2</button>
<button>3</button>
<button>4</button>
<button>5</button>
<button>6</button>
<button>7</button>
<button>8</button>
<button>9</button>
const buttons = document.getElementsByTagName("button");
for (var i = 0 ; i < buttons.length ; i += 1) {
const button = buttons [i];
button.addEventListener ("click", function() {
alert ("Button" + i + "Pressed");
});
}
Here is the link of the video
https://teamtreehouse.com/library/using-let-with-for-loops-2
1) Why they call var i = 0 global variable? it exist in for loop, why not call block variable?
2) If var i = 0 is global variable, when first time clicking button.addEventListener it show 10 not 0 ?
1 Answer
Gabbie Metheny
33,778 PointsVariables that use the var keyword have global scope, which can be probematic when you want them to only have local access (i.e. inside of a function block). This is part of why the let and const keywords were introduced in ES6: both let and const have block scope, which means they only exist and are accessible within the block they were created.
In the example from the video, when using var, i starts at 0, and increases by 1 on each time through the loop. Since it exists globally, you can't have a unique i for each button, and so you get the final value (10) for each.
const buttons = document.getElementsByTagName("button");
for (var i = 0 ; i < buttons.length ; i += 1) {
const button = buttons[i];
button.addEventListener ("click", function() {
alert ("Button " + i + " Pressed");
});
}
// first iteration: i = 0, i += 1, now i = 1
// second iteration: i = 1, i += 1, now i = 2
// third iteration: i = 2, i += 1, now i = 3
// fourth iteration: i = 3, i += 1, now i = 4
// fifth iteration: i = 4, i += 1, now i = 5
// sixth iteration: i = 5, i += 1, now i = 6
// seventh iteration: i = 6, i += 1, now i = 7
// eighth iteration: i = 7, i += 1, now i = 8
// ninth iteration: i = 8, i += 1, now i = 9
// tenth iteration: i = 9, i += 1, now i = 10
// outcome when any button is pressed: "Button 10 Pressed"
If you change the code to use let, each iteration through the loop gets its own local copy of i that is not accessible to the other iterations through the loop or to any part of the code. So the first iteration gets its own i, which will always be 0. Then after the end of that iteration through the loop, i increments, and the next iteration through gets its own i, which will always be 1, and so on.
Basically, it's not recommended to use var any more because it causes all kinds of problems, like the one Andrew addressed in the video. const is useful when you don't want the value of your variable to change, and let can be used when you need some flexibility (like in iteration, for example), but both have block scoping rather than global. Check out Fun Fun Function's video on var, let and const for more examples, or the MDN docs on let and const.
Let me know if you need further clarification!
Tani Huang
6,952 PointsTani Huang
6,952 PointsI got misunderstand of for loop, thought it has same way with function can let variable become local variable. but is clear now. thank you so much.
Gabbie Metheny
33,778 PointsGabbie Metheny
33,778 PointsYou bet! Glad it's making sense!