Falsy Bouncer null problem

Tell us what’s happening:
ok i don’t understand. In the task where you want to remove all falsy variables, I went the stupid way, and just, with a loop, I started sorting through all the data in the array. Everything is fine, except null, for some reason, if is not accept " if meh[i] !== null, and only null. I don’t understand why and I don’t know how to look for an explanation of this.
p.s.
sorry for bad english
p.p.s.
well, now i realise how easy is solution, but i still don’t understand, why is this code dont work, why null?

Your code so far


function bouncer(arr) {
let meh = [];
for (let i = 0; i < arr.length; i++) {
  if (arr[i] !== false && arr[i] !== null && arr[i] !== 0 && arr[i] !== "" && arr[i] !== NaN && arr[i] !== undefined) {
    meh.push(arr[i]);
  }
}
return meh;
}

console.log(bouncer([false, null, 0, NaN, undefined, ""]));

this return [null]

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36.

Challenge: Falsy Bouncer

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/falsy-bouncer

The browser is actually logging [NaN], not [null]. You can see it if you opened the browser console.

You can’t just use === or !== when comparing NaN, because it’s the only value that’s not equal to itself (NaN === NaN is actually false!). Use Number.isNaN() to check if a value is NaN.

Alternatively, look at the instruction hint!

1 Like

thanks for the explanation! I try to find a solution, and only then look at the hints. when I remembered that if on its own accepts only true value, I looked at hints, but could not find an explanation :slight_smile: thanks again!

there is a simpler way, using the property of being falsy: if you coerce to boolean it will evaluate as false
so you have !NaN being true (because the ! flip the boolean value), and !!NaN being false (double flip!)

You don’t even need to coerce it to boolean – the identity function x=>x will work just as well.