+ 13

Nested for loops in JS

I can't figure out nested for loops. My head is spinning when I see two for loops one after the other. Can someone explain please? Am I missing a lesson or another explanation here? Thank you: var arr = [[1,2], [3,4], [5,6]];: for (var i=0; i < arr.length; i++) { for (var j=0; j < arr[i].length; j++) { console.log(arr[i][j]); } }

8th Mar 2017, 1:26 PM
‎‏‎‏‎Joe
‎‏‎‏‎Joe - avatar
3 Antworten
+ 13
It will start from the outer or top loop, looping 3 times as there are 3 arrays. It well enter each array or namely there indexes - for example arr[0][1] would be equal to 2 in this case - and output each index to the console.
8th Mar 2017, 2:26 PM
Mark Foxx
Mark Foxx - avatar
+ 12
thank you Mark!
8th Mar 2017, 4:46 PM
‎‏‎‏‎Joe
‎‏‎‏‎Joe - avatar
+ 12
so i = 3 and j = 2 (length of each array)?
9th Mar 2017, 8:09 AM
‎‏‎‏‎Joe
‎‏‎‏‎Joe - avatar