-1

Possible Duplicate:
JavaScript for…in vs for
difference between for..in and for loops, and counter declaration

Is there any particular reason that for loops in arrays should be coded like for (var i = 0; i < foo.length; i++) { whereas loops in objects are just for (var i in foo) { Is it because of how objects are set up vs how arrays are set up? (I know arrays are a type of object btw) or just another nitpicky programming convention.

Community
  • 1
  • 1
Kpower
  • 1,237
  • 3
  • 11
  • 19
  • 1
    actually that's not a good duplicate, so I'll look for one of the 8 billion other ones ... – Pointy Aug 16 '12 at 22:44
  • Little half-baked xample: http://jsfiddle.net/XCrUx/ – biziclop Aug 16 '12 at 22:47
  • The question itself [here](http://stackoverflow.com/questions/2992295/correct-use-of-a-for-in-loop-in-javascript) is not much of an overlap, but the inestimable T.J.Crowder's answer covers all the bases. – Pointy Aug 16 '12 at 22:48
  • @MaVRoSCy I don't know if I'm misunderstanding this but http://w3schools.com/js/js_obj_array.asp says (or implies) that arrays are a type of object... – Kpower Aug 16 '12 at 22:49
  • 1
    @Kpower yes arrays are objects, but they're special, and [please be careful with w3schools](http://w3fools.com). – Pointy Aug 16 '12 at 22:52
  • @MaVRoSCy how exactly aren't arrays a type of object..? – Phillip Schmidt Aug 16 '12 at 22:53
  • @Pointy so should I stick to MDN from now on ;) – Kpower Aug 16 '12 at 22:54
  • @Kpower ha ha well yes MDN tends to be a lot better, plus it's basically a Wiki so if you find an error you can fix it. – Pointy Aug 16 '12 at 22:55

2 Answers2

2

You should use for(i=0; i<n; i++) looping when you are iterating a list, as they are indexable by integer.

arr = [1,2,3,4]; // arr[0] == 1; arr[1] == 2;
for(var i=0; i<arr.length; i++) { alert(arr[i]; } // 1, 2, 3, 4

You should use the for...in looping when you are iterating an object, as it is indexable by key. If you think about an object as a key:value map. Remember to check if the object has it's own property matching each key, as the in statement will check down the object's proptotype chain and return property names inherited from above.

obj = { 
  foo: 1,
  bar: 2
}
for(key in obj) {
  if(obj.hasOwnProperty(key)) {
    alert(key);       // foo, bar
    alert(obj[key]);  // 1, 2
  }
}
Aesthete
  • 18,622
  • 6
  • 36
  • 45
-1

Well, the main reason is because for...in only iterate over defined members.

So,

var arr = [];
arr[10] = "hi";
for(var x in arr)
{
    alert(x);
}

will only show the one value that is defined. Sometimes this is a good thing-- usually not.

Also, for...in loops over properties, not array indices (in javascript an array is just an a regular object with properties whose names are the values you assign to the array)

So when you use for..in you can get weird results, like:

1

2

3

length

toString

etc.

Using the typical for syntax is just the better option for arrays. With objects, the for...in makes more sense, since the undefined issues don't come up as often. Either way, I like to use the for(var x; x < y; x++) syntax, though. It just feels better in js.

Phillip Schmidt
  • 8,805
  • 3
  • 43
  • 67
  • Not "in the past", a for..in loop iterates over all **enumerable** properties of an object, including those on its `[[Prototype]]` chain. The numeric indices of an Array are just numeric object properties that are treated differently by Array methods. – RobG Aug 17 '12 at 01:30
  • It's not weird behaviour, it's expected. – Aesthete Aug 17 '12 at 01:39
  • @Aesthete and being expected means it can't also be weird? – Phillip Schmidt Aug 17 '12 at 14:11
  • @RobG and yeah, I know. Probably not the best wording on my part, but believe it or not I did have the right idea – Phillip Schmidt Aug 17 '12 at 14:12