Falsy Bouncer, isn't NaN falsy in JavaScript?

Falsy Bouncer, isn't NaN falsy in JavaScript?
0

#1

Tell us what’s happening:
I found it very surprising to notice that: NaN == false return false and also NaN == true return false. I thought NaN is falsy!

Your code so far


function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  let myArray = [];
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] != false)
      myArray.push(arr[i]);
    
  }
  console.log(myArray);
  return myArray;
}

bouncer([7, "ate", "", false, 9]);

Your browser information:

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

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


#2

Actually NaN will return false even if you compare it with another NaN. You can check for NaN with Number.isNaN().


#3

i found it a bit weird because I thought NaN == false will return true! because you know NaN is falsy and Java Script will do the type coercion thing and it will be like false == false, which would be true!


#4

NaN is if the value should be a number, but its not. So all JS ‘knows’ is that it’s dealing with a thing that it doesn’t understand. For example, Number('hi') — JS will attempt to directly convert a string to a number, fail, and return NaN. JS won’t keep hold of the input to the Number function and exhaustively check what it could possibly be. That’s possibly the the best way to grok why it doesn’t equal itself — one unknown thing can’t equal another unknown thing