title: What's the best way to iterate over items in javascript?
body: <p>I recently came accross a different way of looping though an array in Javascript. </p>
  
  <p>I used to write loops something like this:</p>
  
  <pre><code>for (var len = 0; len &lt; testData.length; len++) {
    total = total + testData[len];
  }
  </code></pre>
  
  <p>I read some code that did it this way:</p>
  
  <pre><code>for (var len = 0; testData[len]; len++) {
    total = total + testData[len];
  }
  </code></pre>
  
  <p>I was wondering how these would perform so I used <a href="http://jsperf.com/for-until-length-vs-until-undefined/10" rel="nofollow noreferrer">jsPerf</a> to find it out. The results are pretty amazing. I expected the second method to be a bit it faster than the first, but it's actually much, much faster.</p>
  
  <p>Is there a down side I'm missing here? Or is this the best way to loop through the items of a list.</p>
  
  <p><strong>Update:</strong></p>
  
  <p><em>gray state is coming</em> and <a href="https://stackoverflow.com/a/12357692/31722"><em>Diode</em></a> pointed me to a simple flaw in the testcase which made is seem to be faster. </p>
  
  <p>After correcting the mistake, this is the fastest:</p>
  
  <pre><code>var datalen = testData.length;
  for (var len = 0; len &lt;datalen; len++) {
       total = total + testData[len];
  }
  </code></pre>
  
  <p><strong>Update 2:</strong></p>
  
  <p>After testing in some more browsers this testcase takes a different direction again. 
  Only in Chrome and Opera the normal for loop is the fastest. In other all other browsers <em>Shmiddty</em>'s way is just a bit faster.</p>
  
  <pre><code>var i = testData.length, sum=0;
  while (i--){
      sum += testData[i];
  }
  </code></pre>