+ 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]); } }
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.
+ 12
thank you Mark!
+ 12
so i = 3 and j = 2 (length of each array)?