+ 1
JavaScript: why myArr.length = 2
var myArr = []; myArr[0] = [10]; myArr[1] = [20]; myArr.foo = 30; console.log(myArr.length); What is myArr.foo here? Looks like an objectâs property but isnât myArr an array?
2 Answers
+ 3
That's because foo is not a valid array index and just is an ordinary property:
'''
A property name P (in the form of a String value) is an array index if and only if ToString(ToUint32(P)) is equal to P.
'''
+ 1
'length' only refers to the 'enumerable' parts of your object, not to all of them. So in this case only the indexes [0] and [1] are counted in.