JS Input/Output Solution 7

What is the length of an array?

1
2
let arr = [1 , , 3];
console.log(arr.length);   // ?

Solution:

1
2
let arr = [1 , , 3];
console.log(arr.length);   // 3

arr.length is 3 and not 2 because length property only tracks the highest numeric index within an array. It doesn’t loop over an array to count the number of elements. So, any holes within an array will be skipped.

In order to return the count of elements, we might need to write our own custom method.