title: Javascript loop error: Uncaught TypeError
body: <p>I am practicing Javascript and have the following code which returns an <code>Uncaught TypeError: Cannot set property '0' of undefined</code> error on the <code>sets[i][j] = initial_sets [i][j];</code> line. The idea seems to be correct, but I can't figure out why I am getting the error. </p>
  
  <pre><code>var sets = [[],[]]; //Declared this 2D array based on solution in https://stackoverflow.com/questions/16512182/how-to-create-empty-2d-array-in-javascript
  var initial_sets=[[a,b],[c,d],[e,f]]; //2D array
  var i,j;
  
  //2D array sets is being filled by 2D array initial_sets
  for (i=0; i&lt;initial_sets.length; i++) {
      for (j=0; j&lt;initial_sets[i].length; j++) {
              sets[i][j] = initial_sets[i][j];
      }
  }
  </code></pre>
  
  <p>I tried the solution posted in <a href="https://stackoverflow.com/questions/10673237/uncaught-typeerror-cannot-set-property-0-of-undefined">Uncaught TypeError: Cannot set property &#39;0&#39; of undefined &quot;</a> by creating a 1D array and then assigning another dimension to it, but I still get the same error.</p>