I would like to remove all falsy values from an array. Falsy values in JavaScript are false, null, 0, "", undefined, and NaN.
function bouncer(arr) {
arr = arr.filter(function (n) {
return (n !== undefined && n !== null && n !== false && n !== 0 && n !== "" && isNaN()!=NaN); });
return arr;
}
bouncer([7, "ate", "", false, 9, NaN], "");
The above is getting satisfied for all except the NaN test case. Can someone help me check in the array whether it contains NaN or not?
You use
isNaN()
in wrong way. It should be something like following:}
Also you can rewrite it:
Just negate twice to "cast" to boolean.
!NaN === true
=>!!NaN === false
^ that's how you do it
it will return [].