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

10,339

Solution 1

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.

Solution 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.

Solution 3

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.

Share:
10,339
Artur Sapek
Author by

Artur Sapek

artur.co Cryptowatch

Updated on August 22, 2022

Comments

  • Artur Sapek
    Artur Sapek over 1 year

    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){

  • Artur Sapek
    Artur Sapek over 12 years
    Touche! I should have been more specific. I meant when I'm dealing with matrices.
  • Šime Vidas
    Šime Vidas over 12 years
    @Artur By matrix you mean an array of arrays, right? (Because there is no built-in matrix type/class in JavaScript)
  • Artur Sapek
    Artur Sapek over 12 years
    Yes! Sorry, I meant arrays with arrays inside of them, or I suppose even single-layer arrays.
  • user113716
    user113716 over 12 years
    @Artur Sapek: In that case, don't use for-in on an Array in JavaScript. The for statement is correct.
  • Artur Sapek
    Artur Sapek over 12 years
    @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]); }
  • user113716
    user113716 over 12 years
    @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.