You can check if there is a value NaNusing the function isNaN:
var result = average();
if (isNaN(result)) result = 0;
In the future, when ECMAScript 6 will be widely available, you might consider switching to Number.isNaN, because it does isNaNnot properly treat strings as parameters.
isNaN, Number.isNaN . , , NaN, , NaN. , number, NaN, true.
:
isNaN("a");
Number.isNaN("a");