Array.length gives incorrect length

后端 未结 4 1915
我在风中等你
我在风中等你 2020-12-03 16:41

If I have an array having object as values at the indices like:

var a = [];
a[21] = {};
a[90] = {};
a[13] = {};
alert(a.length); // outputs 91
4条回答
  •  星月不相逢
    2020-12-03 17:43

    Because that's the behavior of Array.length as described in the ECMAScript spec.

    The length property of this Array object is a data property whose value is always numerically greater than the name of every deletable property whose name is an array index.

    So Array.length is always the last item's index + 1.

提交回复
热议问题