title: best way to generate empty 2D array
body: <p>is there a shorter, better way to generate 'n' length 2D array?</p>
  
  <pre><code>var a = (function(){ var i=9, arr=[]; while(i--) arr.push([]); return arr })();
  
  a // [ [],[],[],[],[],[],[],[],[] ]
  </code></pre>
  
  <p>** old-school short-way**:</p>
  
  <pre><code>var a = (function(a){ while(a.push([]) &lt; 9); return a})([]);
  </code></pre>
  
  <p><strong>UPDATE - Using ES2015</strong></p>
  
  <pre><code>Array(5).fill().map(a=&gt;[]); // will create 5 Arrays in an Array
  // or
  Array.from({length:5}, a=&gt;[])
  </code></pre>
  
  <p><strong>Emptying 2D array</strong> (saves memory rather)</p>
  
  <pre><code>function make2dArray(len){
      var a = [];
      while(a.push([]) &lt; len); 
      return a;
  }
  
  function empty2dArray(arr){
      for( var i = arr.length; i--; )
        arr[i].length = 0;
  }
  
  // lets make a 2D array of 3 items
  var a = make2dArray(3);
  
  // lets populate it a bit
  a[2].push('demo');
  console.log(a); // [[],[],["demo"]]
  
  // clear the array
  empty2dArray(a);
  console.log(a); // [[],[],[]]
  </code></pre>