title: Javascript Generic For Loop
body: <p>Is there any way to create a generic for loop that will loop through either an array or an object correctly? I know I can write the following for loop, but it will also loop through other properties that would be added to an array.</p>
  
  <pre><code>for (item in x) {
     console.log(item)
  }
  </code></pre>
  
  <p>By this I mean a for loop that will iterate:</p>
  
  <pre><code>x = [1, 2]
  x.foo = "foo"
  y = {first:1, second: 2}
  </code></pre>
  
  <p>x as </p>
  
  <pre><code>1
  2  
  </code></pre>
  
  <p>y as </p>
  
  <pre><code>first
  second
  </code></pre>
  
  <p>The reason behind this is that I won't know until runtime what x will be (either an Array or an Object). Is my only option to create a function that will check at runtime?</p>