+ 1

JavaScript: Why does arr length is 42?

var a = []; a[0] = 0; a.push(1); a[41] = 41; console.log(a.length); //42 // console.log(a) outputs only three elements: [0,1,41] How does js count array length?

9th Feb 2020, 8:23 AM
Prof. Dr. ZoltĂĄn Vass
2 Answers
+ 2
it start from index 0. so count the 0 too
9th Feb 2020, 8:40 AM
Taste
Taste - avatar
+ 1
Because index start from 0 and last index is 41 that's why length is 42.
9th Feb 2020, 8:40 AM
A͢J
A͢J - avatar