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

前端 未结 7 834
走了就别回头了
走了就别回头了 2020-12-13 18:43
  1. Why NaN === false => false, isn\'t NaN falsy?
  2. Why NaN === NaN => false, but !!NaN === !!NaN => true

I\'v

7条回答
  •  感情败类
    2020-12-13 19:29

    1. Why NaN === false => false, isn't NaN falsy?

    NaN as you are using, is a global property initialized with value of Not-A-Number. It's not boolean. It's NaN data type as defined by IEEE 754.

    It's the "same thing" you compare null === false (or even null == false).

    In this case, there is no difference using sctric equal or not: NaN == false, also will return false!

    1. Why NaN === NaN => false, but !!NaN === !!NaN => true

    2.1. NaN ==== NaN, is false by definition.

    2.2. But in !!NaN === !!NaN, you aren't comparing NaNs anymore, when you do ![value], you "evaluate" it (or cast to a boolean).

    I'm gonna now explain with null, because it's more used, so you can apply it to NaN:

    Casting NaN, it's the same thing that casting null.

    null == false? true : false     // false! because it's not a bool false.
    !null? true: false // true! because !null -> true -> if(true)...

    More Refs:

    http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.1.1
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN

提交回复
热议问题