# Could someone tell me if I understand this properly?

**URL:** <https://forum.freecodecamp.org/t/could-someone-tell-me-if-i-understand-this-properly/13429>\
**Category:** General\
**Created:** [June 22, 2016, 2:13pm UTC](https://forum.freecodecamp.org/t/could-someone-tell-me-if-i-understand-this-properly/13429 "2016-06-22T14:13:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dorian4321](https://avatars.discourse-cdn.com/v4/letter/d/df788c/32.png) [@dorian4321](https://forum.freecodecamp.org/u/dorian4321)\
**Post date:** [June 22, 2016, 2:13pm UTC](https://forum.freecodecamp.org/t/could-someone-tell-me-if-i-understand-this-properly/13429/1 "2016-06-22T14:13:27Z")

</div>

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]);  
}  
}

firstly i === 0 so we check the length of arr which is 3 and i becomes one. i is now the first part of the arr ([1,2]). Then j runs against i and comes back with 1 as i === 1 so now j === i === 1 so the console returns arr[i][j] which === arr[1][1] which is arr[1][2].

am I way off the mark here? I seem to be having a real struggle with for loops. If its wrong can someone break the code down for me and explain it please. Thanks a lot.

---

<div class="post-metadata">

**Author:** ![PortableStick](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/portablestick/32/65591_2.png) [@PortableStick](https://forum.freecodecamp.org/u/PortableStick)\
**Post date:** [June 22, 2016, 2:27pm UTC](https://forum.freecodecamp.org/t/could-someone-tell-me-if-i-understand-this-properly/13429/2 "2016-06-22T14:27:06Z")

</div>

You’re not too far off, but you’re numbers are wrong.

```
for(var i = 0; i < arr.length; i++)

```

Your first couple of points are correct. `i` is declared and initialized to be 0. Then it’s checked against the length of the array `arr`, which is 3. However, `i` doesn’t increment here. If `i` is less than the length of `arr`, it executes the code in the for loop, which is another for loop in this case.

for(var j = 0; i \< arr[i].length; j++)

`j` is declared and initilized to be 0. The it’s checked against the length of the array that occupies the first index of `arr`, which is 2. If `j` is less than that length, it executes the code inside the for loop, this time a call to `console.log()`. At this point, `i` and `j` both have the value of 0. Once the `console.log()` function is done, we return to the second for loop and complete the next instruction, `j++`. `j` is again checked against the length of `arr[0]` - `j` is 1 and the length of `arr[i]` is 2, so the inequality is true and we execute the code in the second for loop again. This process is repeated until all of the for loops are complete.

Does that help at all?

---

<div class="post-metadata">

**Author:** ![PortableStick](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/portablestick/32/65591_2.png) [@PortableStick](https://forum.freecodecamp.org/u/PortableStick)\
**Post date:** [June 22, 2016, 2:35pm UTC](https://forum.freecodecamp.org/t/could-someone-tell-me-if-i-understand-this-properly/13429/5 "2016-06-22T14:35:46Z")

</div>

Well, I took a bunch of shortcuts. Your answer is way clearer than mine.

---

<div class="post-metadata">

**Author:** ![dorian4321](https://avatars.discourse-cdn.com/v4/letter/d/df788c/32.png) [@dorian4321](https://forum.freecodecamp.org/u/dorian4321)\
**Post date:** [June 22, 2016, 2:52pm UTC](https://forum.freecodecamp.org/t/could-someone-tell-me-if-i-understand-this-properly/13429/7 "2016-06-22T14:52:22Z")

</div>

Such a great feeling when something just clicks in your mind. Had to stare at it for a good ten minutes but I get it now. Thanks both.

---

<div class="post-metadata">

**Author:** ![jwilkins.oboe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jwilkins.oboe/32/179497_2.png) [@jwilkins.oboe](https://forum.freecodecamp.org/u/jwilkins.oboe)\
**Post date:** [January 16, 2021, 3:47am UTC](https://forum.freecodecamp.org/t/could-someone-tell-me-if-i-understand-this-properly/13429/8 "2021-01-16T03:47:25Z")

</div>


