3

I think there must be a question like this before but current search engines cannot give you results to queries of code lines.

My question is:

What's the difference between

for ( var i = 0; i < array.length; i ++ )
    console.log(array[i]);

and

for ( var e in array )
    console.log(e);

In my case, the first returns 'undefined' or sequence of number, while the second works fine.

SolessChong
  • 3,370
  • 8
  • 40
  • 67
  • 2
    I looked up "for in vs for" just like that and first result I got in Google was http://stackoverflow.com/questions/5263847/javascript-loops-for-in-vs-for – elclanrs Aug 21 '13 at 03:32
  • Can you show the array? It sounds like an object. – bfavaretto Aug 21 '13 at 03:40
  • After bfvaretto's comment and reading your last statement, you should provide a SSCCE where the first snippet fails. – Fabrício Matté Aug 21 '13 at 03:41
  • 1
    Also, the first snippet is supposed to log array *values*, while the second logs *keys*. – bfavaretto Aug 21 '13 at 03:43
  • 2
    `for` is significantly faster than `for...in`; also, for performance reasons, this is a better way of writing the condition: `for (var i = 0; l = array.length; i < l; i++)` and finally, if you want to make the `for` loop even more effective, reverse its order: `for (var i = items.length; i--;)` – Dimitri Vorontzov Aug 21 '13 at 03:44
  • @elclanrs A good lesson – SolessChong Aug 21 '13 at 04:32
  • @bfavaretto `while the second logs keys` I think this is the exact and concise answer. – SolessChong Aug 22 '13 at 02:53
  • Glad to know you found what was confusing you. But don't disregard the answers you got below, you have really valuable advice in there. If I were you, I would accept one of them. – bfavaretto Aug 22 '13 at 03:02

3 Answers3

2

for...in can actually be a problem when iterating javascript arrays. It iterates over all the property names in the array, so it may hit more than the actual array values. If you were to say

 array.custom ="x"

then it would print "custom" along with any other properties and all the indices of the array.

The other result goes over the length of the array and will pass the indices of each item in the array. It is the safer way to iterate over arrays in Javascript.

Ben McCormick
  • 25,260
  • 12
  • 52
  • 71
2

Well the first one goes through the indices of the elements in the array. The second one goes through the properties and methods in any object, given an array is also a regular object which has "special" native features (the numerical indexing of elements) it can also go through the second loop as any other object could, the difference is: In the first way, the elements indexed in the array will show up. In the second way, not only that will be iterated over, but also any other property and method that the array object has inherited.

Delta
  • 4,308
  • 2
  • 29
  • 37
0

I beleive the real question here should be what's the differenct between:

for ( var i = 0; i < array.length; i ++ )
    console.log(array[i]);

and:

for ( var e in array )
    console.log(array[e]);

because you should be comparing 2 functions that are supposed to display the same thing.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83