+ 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?

9th Feb 2020, 10:22 AM
Prof. Dr. ZoltĂĄn Vass
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. '''
9th Feb 2020, 11:05 AM
Qasem
+ 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.
9th Feb 2020, 10:57 AM
HonFu
HonFu - avatar