Does JavaScript array.forEach traverse elements in ascending order

前端 未结 3 461
陌清茗
陌清茗 2020-12-29 19:48

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

3条回答
  •  佛祖请我去吃肉
    2020-12-29 20:16

    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.

提交回复
热议问题