Nesting for Loops

Can anyone explain to me why I is a subarray here? Is it because I =0? but then we also declared J = 0 so im a bit confused?

const arr = [
  [1, 2], [3, 4], [5, 6]

for (let i = 0; i < arr.length; i++) {
  for (let j = 0; j < arr[i].length; j++) {

This outputs each sub-element in arr one at a time. Note that for the inner loop, we are checking the .length of arr[i], since arr[i] is itself an array.

1 Like

hi Enda, I think I responded to an earlier question you made about js also.
Did you understand that response okay? (didn’t get any response from you so just checking)

1 Like

Hi! If I may explain it a bit.

First Array —> [ [1,2], [3,4], [5,6] ].

When you use the first for loop with i, you are accessing subarrays from that first array. So console.log(arr[i]) should be [1,2], [3,4], [5,6].

Then when you use the second loop with j, you are accessing subarrays from the first subarrays, which is arr[i].

So console.log(arr[i][j] should be:
1 and 2 for [1,2]
3 and 4 for [3,4]
5 and 6 for [5,6]

Hope this helps!

1 Like

can you private message me? i need some further clarification :frowning: