Take the 2-minute tour ×

Given a simple zero based, numerically indexed array:

var list = ['Foo', 'Bar', 'Baz'];

Many times, I have noticed that when someone suggests looping through variables in an array like this:

for(var item in list) { ... }

...there's almost certainly someone suggesting that that's bad practice and suggests an alternative approach:

var count = list.length;

    var item = list[i];

What's the reasoning for not using the simpler version above and to use the second example instead?

share|improve this question
You're not looping over the items with that loop, you're looping over the keys / property names / indizes. –  Bergi Jul 25 '12 at 11:08
Also C coders in victorian tophats not understanding iiterators. However keep in mind its spitting out keys, not values. The for( ; ; ) format is faster, but 99% of the time, it really does not matter. Coder time is more expensive than computation time unless your working on megaprojects or stuff with a genuine need of optimization. –  Shayne Sep 16 at 2:39

4 Answers 4

up vote 53 down vote accepted

First, the order of the loop is undefined for a for...in loop, so there's no guarantee the properties will be iterated in the order you want.

Second, for...in iterates over all enumerable properties of an object, including those inherited from its prototype. In the case of arrays, this could affect you if your code or any library included in your page has augmented the prototype of Array, which can be a genuinely useful thing to do:

Array.prototype.remove = function(val) {
    // Irrelevant implementation details


for (var i in a) {

// Logs 0, 1, 2, "remove" (though not necessarily in that order)
share|improve this answer
thats fine as long as you use hasOwnProperty though - for (var i in a) { if (a.hasOwnProperty(i)) console.log(i); } -> 1 2 3 –  Dimitar Christoff Feb 15 '10 at 10:31
One suggestion, change over all properties to over **enumerable** properties. In newer javascript implementations, properties can be defined with the enumerable attribute set to false. These properties and properties of built-in javascript objects wouldn't show up in a for...in. –  Andy E Feb 15 '10 at 10:37
@Dimitar: Indeed, although once you've added that in, the loop has stopped looking simpler than a standard C-style for loop. –  Tim Down Feb 15 '10 at 10:39
@Andy E: You're right. I will change it. –  Tim Down Feb 15 '10 at 10:40
anyway - the only legitimate reason for looping an array like that is to obtain the array keys (which works due to nature of arrays in javscript) - any other reason, you better do a normal loop. –  Dimitar Christoff Feb 15 '10 at 10:43


for(..;..;..) loop proved to be 36 times faster than for .. in when I tested it here.

Link courtesy this SO answer

share|improve this answer
I misread that as infinite loop is 36x faster than a normal loop. Thanks for the link +1. –  Thomas O Oct 3 '10 at 13:10
@Thomas Reasonable misunderstanding - when read out of context. Fixed :) –  Amarghosh Oct 4 '10 at 4:41

If you use for/in like that, item enumerates through string values "0", "1", ..., so not the actual objects in the list. So the the 'item' in the first snippet is more like the i in the second snippet,not the item. Furthermore string values are enumerated where you'd expect numbers. And you get in trouble when you properties to the list, like array.ID = "a123", as they will get enumerated also.

But with these downsides, I still think the syntax is very useful, if your team is aware of what it does.

share|improve this answer

Add list.foo = bar; and try to use simple for. If you don't use some libraries(like prototypeJs) and don't add any new properties to array object - you can use simple for-statement.

share|improve this answer

Your Answer


