title: Undefined values with new Array() in JavaScript
body: <p>Looking at some javascript code, I saw (something like) this:</p>
  
  <pre><code>var arr = Array.apply(null, {length: 10});
  </code></pre>
  
  <p>Reading the MDN documentation for Function.prototype.apply(), I learnt that although it usually expects an array as its second argument which is an array of the arguments to be passed to the invoked function,</p>
  
  <blockquote>
    <p>you can also use any kind of object which is array-like, so in practice this means it's going to have a property length and integer properties in the range (0...length).</p>
  </blockquote>
  
  <p>So from what I could tell, it's calling Array() as if it was passed 10 arguments, but since it doesn't define any of those "integer properties", it's as if it was passed 10 undefined arguments. <strong>Is that correct?</strong></p>
  
  <pre><code>console.log(arr);
  </code></pre>
  
  <p>yields</p>
  
  <blockquote>
    <p>[ undefined,
      undefined,
      undefined,
      undefined,
      undefined,
      undefined,
      undefined,
      undefined,
      undefined,
      undefined ]</p>
  </blockquote>
  
  <p>Which is totally different to the result of</p>
  
  <pre><code>var barr = new Array(10);
  console.log(barr);
  </code></pre>
  
  <p>which is </p>
  
  <blockquote>
    <p>[ , , , , , , , , ,  ]</p>
  </blockquote>
  
  <p>These arrays also behave differently.</p>
  
  <pre><code>console.log(arr.map(function(item) { return 'hi';}));
  console.log(barr.map(function(item) { return 'hi';}));
  </code></pre>
  
  <p>gives</p>
  
  <blockquote>
    <p>[ 'hi', 'hi', 'hi', 'hi', 'hi', 'hi', 'hi', 'hi', 'hi', 'hi' ]</p>
    
    <p>[ , , , , , , , , ,  ]</p>
  </blockquote>
  
  <p>I wanted to know why the map function didn't work for the second one. So I checked <code>console.log(1 in arr);</code> which gave <em>true</em> and `console.log(1 in barr);' which gave <em>false</em>.</p>
  
  <p>So my current guess is that arr is an array which contains as integer properties 10 variables which each have the value of <em>undefined</em> and barr is an array which, although it has a length of 10, has no integer properties. And Array.apply works because it asks {length: 10} what its property <em>0</em> is, receives <em>undefined</em>, and so assigns <em>undefined</em> to the property <em>0</em> of the array it's constructing, and so on. Is my reasoning correct? And is there really no less hackish way to define an array which contains undefined integer properties for each index within its range, rather than having no integer properties at all? Because looking at this, it seems to me that <code>new Array()</code> is pretty useless.</p>