Given following array:
var arr = [undefined, undefined, 2, 5, undefined, undefined];
I\'d like to get the count of elements which are
An array length
is not the number of elements in a array, it is the highest index + 1
. length
property will report correct element count only if there are valid elements in consecutive indices.
var a = [];
a[23] = 'foo';
a.length; // 24
Saying that, there is no way to exclude undefined elements from count without using any form of a loop.