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

前端 未结 7 819
走了就别回头了
走了就别回头了 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:46

    This condition:

    NaN === false
    

    Is always false because numbers are not booleans. To test if a value is falsy you can use a ternary expression:

    NaN ? "truthy" : "falsy" // falsy
    

    Why NaN === NaN => false

    This is explained in MDN; pragmatically speaking, though, two values of which you only know they're not numbers can't logically be the same thing.

    ... but why is !!NaN === !!NaN => true

    This is because casting NaN into a boolean will make it false and booleans can be compared as per normal.

    0 讨论(0)
提交回复
热议问题