2

Possible Duplicate:
for(var i in aArray) VS for(i=0; i<aArray.length; i++)

Is it safe to assume that these will always do the same thing, and that I should always use the latter?

for (var i = 0; i < a.length; i++){

for (var i in a){

Community
  • 1
  • 1
Artur Sapek
  • 2,425
  • 6
  • 27
  • 29

3 Answers3

7

No it isn't safe, because they're not the same thing.

The first iterates numeric indices up until whatever value is stored in the .length property in numeric order.

The second enumerates all enumerable properties on an object, including prototyped properties, and doesn't guarantee any sort of order.

Take an Array:

var arr = ['a','b','c'];

Now add something to the prototype object of its constructor function:

Array.prototype.sayHello = function() { alert('hi'); };

If you use a for-in statement, you'll end up hitting the sayHello function instead of just the numeric indices.


If you're dealing with objects that don't have enumerable properties aside from the indices, and you don't care about the order, then it really won't matter I guess, but the proper form is still to use a for statement.

user113716
  • 318,772
  • 63
  • 451
  • 440
  • Touche! I should have been more specific. I meant when I'm dealing with matrices. – Artur Sapek Aug 28 '11 at 20:32
  • @Artur By matrix you mean an array of arrays, right? (Because there is no built-in matrix type/class in JavaScript) – Šime Vidas Aug 28 '11 at 20:35
  • Yes! Sorry, I meant arrays with arrays inside of them, or I suppose even single-layer arrays. – Artur Sapek Aug 28 '11 at 20:38
  • @Artur Sapek: In that case, don't use `for-in` on an Array in JavaScript. The `for` statement is correct. – user113716 Aug 28 '11 at 20:39
  • @patrick dw That's what I mean though, they both do the same thing in several cases in my code. If I'm applying DOM elements to parents from an array: `x = [[parent1, child1], [parent2, child2]...]`, a `for-in` loop does the job. `for (entry in x){ x[entry][0].appendChild(x[entry][1]); }` – Artur Sapek Aug 28 '11 at 20:42
  • @Artur Sapek: So if you're saying that the order of iteration doesn't matter, you still need to deal with issues if you (or any code you're running) decides to extend `Array.prototype`. Suddenly your code will break. In JavaScript, `for-in` is simply the wrong tool for the job when it comes to Arrays. Is there some reason you don't want to use the proper iterator? I don't see the advantage of using a `for-in` enumerator in this case. – user113716 Aug 28 '11 at 20:52
2

No, because (assuming in the first version you use i to index a) arrays can have non-integer indexes. So if you had:

a = {1:1, 'f': 'hey'}

In the first version, i would be 0 then 1. You'd try first to index a with 0 and not get anything back because 0 isn't a valid index, then try with 1 and get 'hey'. So you can see this is clearly wrong.

The second one will go over every index no matter what it is.

So the first version assumes all the indices are numeric and in order, whereas the second will iterate every index no matter what it is.

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
2

No.

Use the former for arrays when you need to enumerate through the natural order of an array. Use the latter for objects, when the ordering is unimportant or inexistant.

Björn
  • 29,019
  • 9
  • 65
  • 81