Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is NaN falsy? Why NaN === false returns false

Tags:

javascript

  1. Why NaN === false => false, isn't NaN falsy?
  2. Why NaN === NaN => false, but !!NaN === !!NaN => true

I've been racking my brain trying to figure this out.

like image 990
sc1013 Avatar asked Mar 24 '14 02:03

sc1013


People also ask

Why does NaN === NaN return false?

Although either side of NaN===NaN contains the same value and their type is Number but they are not same. According to ECMA-262, either side of == or === contains NaN then it will result false value.

Is NaN === NaN?

NaN is not equal to NaN! Short Story: According to IEEE 754 specifications any operation performed on NaN values should yield a false value or should raise an error. Thanks CJ J for sharing this.

Does NaN return true or false?

isnan() isNaN() method returns true if a value is Not-a-Number.


1 Answers

  1. Falsy and being strictly equal to false are very different things, that's why one has a y instead of an e. ;)
  2. NaN is spec'd to never be equal to anything. The second part of your question is comparing false === false, which is funnily enough, true :)

If you really want to know if something is NaN, you can use Object.is(). Running Object.is(NaN, NaN) returns true.

like image 117
alex Avatar answered Sep 18 '22 03:09

alex