title: Understanding nested for loops in javascript
body: <p>I'm learning JavaScript at the moment on freecodecamp and they have an example for nested for loops in one of their excercises:</p>
  
  <pre><code> var arr = [[1,2], [3,4], [5,6]];
   for (var i=0; i &lt; arr.length; i++) {
    for (var j=0; j &lt; arr[i].length; j++) {
      console.log(arr[i][j]);
    }
  }
  </code></pre>
  
  <p>With console.log = 1 2 3 4 5 6 undefined.</p>
  
  <p>I understand for loops more or less, and I understand that [i] and [j] are used to access the array (I think?). I just don't understand why at the end it just prints out those numbers? I found this question asked a few years back but it just explains how to write them, not how they work:</p>
  
  <p><a href="https://stackoverflow.com/questions/10021847/for-loop-in-multidimensional-javascript-array">For loop in multidimensional javascript array</a></p>
  
  <p>I broke it down into:</p>
  
  <pre><code>var arr = [  [1,2], [3,4], [5,6]];for (var i=0; i &lt; arr.length; i++) {
   console.log(arr[i]);}
  </code></pre>
  
  <p>Which prints out </p>
  
  <pre><code>[ 1, 2 ]
  [ 3, 4 ]
  [ 5, 6 ]
  undefined
  </code></pre>
  
  <p>and </p>
  
  <pre><code>var arr = [  [1,2], [3,4], [5,6]];
  for (var i=0; i &lt; arr.length; i++) {  
   for (var j=0; j &lt; arr[i].length; j++) {    
    console.log(arr[i]);  }}
  </code></pre>
  
  <p>which prints out:</p>
  
  <pre><code>[ 1, 2 ]
  [ 1, 2 ]
  [ 3, 4 ]
  [ 3, 4 ]
  [ 5, 6 ]
  [ 5, 6 ]
  undefined
  </code></pre>
  
  <p>and </p>
  
  <pre><code>var arr = [  [1,2], [3,4], [5,6]];
   for (var i=0; i &lt; arr.length; i++) {  
    for (var j=0; j &lt; arr[i].length; j++) {    
     console.log(arr[j]);  }}
  </code></pre>
  
  <p>which prints out</p>
  
  <pre><code>[ 1, 2 ]
  [ 3, 4 ]
  [ 1, 2 ]
  [ 3, 4 ]
  [ 1, 2 ]
  [ 3, 4 ]
  undefined
  </code></pre>
  
  <p>I understand the first two arr[i]. The loop iterates through the array and prints out the individual elements (in this case an array) and in the second one I guess it just does it twice because there are two loops. What I don't understand is:</p>
  
  <ol>
  <li>why the last array in arr[j] is not printed out (where did the
  [5, 6] go?) </li>
  <li>why arr[i][j] suddenly eliminates the arrays and just
  prints out the numbers</li>
  <li>where the 'undefined' comes from</li>
  </ol>
  
  <p>Could anyone help me out with this and explain the steps the code takes before printing it out in the console? I would really like to understand it but don't even know how to search for this question the right way.</p>