title: Hot to get the loop counter/index using for…of syntax in JavaScript
body: <blockquote>
    <h3>Caution:</h3>
    
    <p>question still applies to <code>for…of</code> loops.> Don't use <code>for…in</code> to iterate over an <strong>Array</strong>, use it to iterate
    over the <em>properties</em> of an object.   That said, this</p>
  </blockquote>
  
  <hr>
  
  <p>I understand that the basic <code>for…in</code> syntax in JavaScript looks like this:</p>
  
  <pre><code>for (var obj in myArray) {
      // ...
  }
  </code></pre>
  
  <p>But how do I get the loop <em>counter/index</em>? </p>
  
  <h3>I know I could probably do something like:</h3>
  
  <pre><code>var i = 0;
  for (var obj in myArray) {
      alert(i)
      i++
  }
  </code></pre>
  
  <h3>Or even the good old:</h3>
  
  <pre><code>for (var i = 0; i &lt; myArray.length; i++) {
      var obj = myArray[i]
      alert(i)
  }
  </code></pre>
  
  <p>But I would rather use the simpler <code>for-in</code> loop. I think they look better and make more sense.</p>
  
  <p>Is there a simpler or more elegant way?</p>
  
  <hr>
  
  <h3>In Python it's easy:</h3>
  
  <pre><code>for i, obj in enumerate(myArray):
      print i
  </code></pre>