Ok, so on a web page, I\'ve got a JavaScript object which I\'m using as an associative array. This exists statically in a script block when the page loads:
v
@curtainDog: NO! NO! Using “for .. in” with Array objects in JavaScript is bad - the entire point of most of the the responses here is that “for .. in” does not guarantee any order. Moreover, “for .. in” will grab properties from an object’s constructor’s prototype and will not iterate over nonexistent keys.
@Samnan: The spec simply says that iteration order is not guaranteed; this means that browsers are free to iterate in sorted order, in order of insertion, in random order, or however else they may. It’s just like a hash table - think of it as random, even if it happens to come out in some apparent order.
I’m fairly sure most browsers look for “for..in” with Array objects and iterate in numeric order so that things don’t break; however, it’s not really doing what most people who use it that way think it’s doing, and it should be avoided unless you specifically need object-type iteration.