Skip to content

Instantly share code, notes, and snippets.

@behrangsa
Last active December 16, 2015 17:49
Show Gist options
  • Save behrangsa/5472950 to your computer and use it in GitHub Desktop.
Save behrangsa/5472950 to your computer and use it in GitHub Desktop.
JavaScript arrays are lolsome :)

Two correct ways to use for/in in JavaScript:

for (var i in a) {
  if (!a.hasOwnProperty(i)) continue; 
  
  // loop body
}

for (var i in a) {  
  if (String(Math.floor(Math.abs(Number(i))))) !== i) continue; // ROFL !?
  
  // loop body
}

The first way is not actually bullet-proof (e.g. when you have an array a and somewhere in your code there's a['foo'] = 'bar', for example).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment