title: Count the number of elements of an array in javascript
body: <p>Consider the following:</p>
  
  <pre><code>var answers = [];
  
  answers[71] = {
      field: 'value'
  };
  
  answers[31] = {
      field: 'value'
  };
  
  console.log(answers);
  </code></pre>
  
  <p>This outputs the <strong>length</strong> of the array as <strong>72</strong> but I was expecting it to return <strong>2</strong>. Here's the output of the script from chrome console:</p>
  
  <p><a href="https://i.stack.imgur.com/3VAzQ.png" rel="noreferrer"><img src="https://i.stack.imgur.com/3VAzQ.png" alt="enter image description here"></a></p>
  
  <p>Any ideas why this is?</p>