Code runs until NaN is added

I am working on finding out truthyOrFalsy() link
Below is my code which compared with the real solution is a cumbersome code and not easy to read.
Nevertheless, I’d like to know why my code misbehaves.
All the val === (argument) I provide - return the right result.
The last val === NaN returns true rather than false .

const truthyOrFalsy = val => {
  if (val === 0 ||
   val === false ||
   val === null ||
   val === undefined ||
   val === "" || 
   val === '' || 
   val === `` ||
   val === -0 ||
   val === NaN
    return 'false';
  } else {
    return 'true';

// Uncomment the line below when you're ready to try out your function
console.log(truthyOrFalsy(NaN)) // Should print false 

MDN docs says:

Or perform a self-comparison: NaN , and only NaN , will compare unequal to itself.

but why this is the case? No clue, and is not clarified in the docs.

thankfully, someone on Stackoverflow knows:

1 Like

great thank you for this.
also when I do run a good code like

const truthyOrFalsy = value => true ? true : false;
while I do get the correct results the IDE complains saying Incorrect:If an argument that evaluates to falsy is passed in to the function, the function should return false

Did you post the right code?

look at your condition:

value => true ? true : false;
//       ^ condition

the condition is true, which is always true?

to my understanding value is not always true because a 0, or null, or undefined, or empty string, will be coerced into a boolean evaluation - and it will be false.
Or am I wrong in this understanding?
I might have missed quotes around the return strings.

Certainly values (like zero and empty strings) are falsy (considered false), while other values are considered true.

but there are much easier ways to convert falsy/truthy to actual Booleans