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
Mahmoud Nasser
5,507 PointsUsing Let with For Loops
We used to declare Variables using var keyword to loop thorough array elements and what was printed on the console was the different elements of the array never a specific element only !! and we didnt have to use let so could someone explain to me am too confused now
5 Answers
Steven Parker
243,656 PointsIf you showed a specific code example it might be more clear what you are asking about.
But the only difference between "let" and "var" is the "scope" of the variable (how much of the program has access to it).
If you use "let", the variable can only be used inside the body of the loop. If you use "var", it can be used both inside the loop and after it. Which one to choose would be determined by the the actual code in and around the loop.
Mahmoud Nasser
5,507 Pointsfor(var i=0;i<array.length;i+=1){ Console.log(array[i]); }
i mean this code is going to show all the elements of the array not only one element like the 10 buttons example that the instructor was showing (and we are using var not let also we were using var in the last course considering loops)
Steven Parker
243,656 PointsUnless "i" conflicts with another declaration in the same code, this particular snippet should perform the same with "let" or "var".
Mahmoud Nasser
5,507 Pointsdo u mean that the instructor could have used var instead of let unless i conflicts with another declaration in the same code ??
Steven Parker
243,656 PointsAs I said, it should perform exactly the same. If this is from something you're working on, perhaps try it as an experiment and see for yourself.
And when asking questions relating to a course, it's always helpful to provide a link to the course page (and a time index if it's a video).
Mahmoud Nasser
5,507 PointsOkay thanks :)
Caleb Kleveter
Treehouse Moderator 37,862 PointsI marked Steven's answer as best because it seems to have resolved your question. You should do this yourself in the future when people resolve other questions you have.
Happy Coding!
Mahmoud Nasser
5,507 Pointssure it is a good practice