title: Why are we allowed to create sparse arrays in JavaScript?
body: <p>I was wondering what the use-cases for code like <code>var foo = new Array(20)</code>, <code>var foo = [1,2,3]; foo.length = 10</code> or  <code>var foo = [,,,]</code> were (also, why would you want to use the <code>delete</code> operator instead of just removing the item from the array). As you may know already, all these will result in sparse arrays. </p>
  
  <p>But why are we allowed to do the above thnigs ? Why would anyone want to create an array whose <code>length</code> is <code>20</code> by default (as in the first example) ? Why would anyone want to modify and corrupt the <code>length</code> property of an array (as in the second example) ? Why would anyone want to do something like <code>[, , ,]</code> ? Why would you use <code>delete</code> instead of just removing the element from the array ? 
  Could anyone provide some use-cases for these statements ? 
  <br><br><br><hr>
  I have been searching for some answers for ~3 hours. Nothing. The only thing most sources (2ality blog, JavaScript: The Definitive Guide 6th edition, and a whole bunch of other articles that pop up in the Google search results when you search for anything like "JavaScript sparse arrays") say is that sparse arrays are weird behavior and that you should stay away from them. No sources I read explained, or at least tried to explain, why we were allowed to create sparse arrays in the first place. Except for You Don't Know JS: Types &amp; Grammar, here is what the book says about why JavaScript allows the creation of sparse arrays:</p>
  
  <blockquote>
    <p>An array that has no explicit values in its slots, but has a length property that implies the slots exist, is a weird exotic type of data structure in JS with some very strange and confusing behavior. The capability to create such a value comes purely from old, deprecated, historical functionalities ("array-like objects" like the arguments object).</p>
  </blockquote>
  
  <p>So, the book implies that the <code>arguments</code> object somehow, somewhere, uses one of the examples I listed above to create a sparse array. So, where and how does <code>arguments</code> use sparse arrays ?
  <br><br><br><hr>
  Something else that is confusing me is this part in the book "JavaScript: The Definitive Guide 6th Edition":</p>
  
  <blockquote>
    <p>Arrays that are sufficiently sparse are typically implemented in a slower, more memory-efficient way than dense arrays are`.</p>
  </blockquote>
  
  <p>"more memory-efficient" appears like a contradiction to "slower" to me, so what is the difference between the two, in the context of sparse arrays especially ? <a href="https://www.safaribooksonline.com/library/view/javascript-the-definitive/9781449393854/ch07s03.html" rel="nofollow noreferrer">Here</a> is a link to that specific part of the book. </p>