In JavaScript I can have an array with holes:
a = [];
a[0] = 100;
a[5] = 200;
a[3] = 300;
a.forEach(function(x) {alert(x);});
I could not
The ECMA-262, 5th edition specification and MDN's Array.forEach() page both show the algorithm for .forEach()
, and it will definitely iterate over array elements in ascending index order (skipping indices that were never assigned a value).
Of course, some browsers may not implement that algorithm properly, but I'm not aware of any that don't.
The specification says forEach
will visit the array elements in numeric order. It doesn't visit elements that don't exist. See the link for details. So for your example array, it will visit element 0
, then 3
, then 5
. The order in which you add them to the array has no effect on the order in which they're visited.
I checked that "for .. in" loop traverses array indices in ascending order, while property names of an object are traversed in the same order they were added to object (at least it looks so).
The order in which for-in
visits object properties is not defined by the specification, not even in ES2015 (aka ES6), despite the fact that ES2015 defines an order for object properties — that order doesn't apply to for-in
or Object.keys
. (More about that in this answer.) If you want to visit properties in the order defined in ES2015, you can use Object.getOwnPropertyNames (for properties that aren't defined with Symbol
names) or Reflect.ownKeys (for both Symbol
and string property names [remember numeric property names are really strings]). Both of those do respect property order.
Straight out of the ECMAScript standard
forEach calls callbackfn once for each element present in the array, in ascending order. callbackfn is called only for elements of the array which actually exist; it is not called for missing elements of the array.
So Array.forEach will skip certain elements in an array. Your example
a.forEach( function( value ) { console.log( value ) }); // prints 100, 300, 200
If you do want to traverse the array in ascending order and all your elements are numbers then you can sort the array beforehand like so
a.sort( function( a, b ) { return a - b });
// this now prints 100, 200, 300
a.forEach( function( value ) { console.log( value ) });