Count of "Defined" Array Elements

The Solution to Count of "Defined" Array Elements is

In recent browser, you can use filter

var size = arr.filter(function(value) { return value !== undefined }).length;


Another method, if the browser supports indexOf for arrays:

var size = arr.slice(0).sort().indexOf(undefined);

If for absurd you have one-digit-only elements in the array, you could use that dirty trick:


There are several methods you can use, but at the end we have to see if it's really worthy doing these instead of a loop.

~ Answered on 2012-03-28 06:33:21

Most Viewed Questions: