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 trialRAJ PATEL
4,282 Pointsincrementing index++
when you are incrementing the index++ in the for-in loop; doesn't that value change each time ONLY in the loop itself(inside the loop brackets). I thought that once the loop is over the value again goes back to 0(index = 0).
1 Answer
Chris Smith
2,991 PointsHi Raj,
You are halfway there...
In a for-in loop the index variable is only visible inside the loop. The technical term for this is "scope", I.e. the scope of the index variable is inside the loop only.
If you try to access the variable outside the loops curly braces it simply does not exist anymore.
This can get confusing if you have a second variable with the same name, for example:
var index = 5
then you have your for-in loop that uses a variable also called index, after the loop has finished index will still equal 5 because it is a completely separate variable.
I hope that helps.
Regards, Chris