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

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

    NaN and false are both the subset of Falsy.

    • NaN == false -> they're both falsy -> thus 'TRUE'
    • NaN === false -> they're NOT the same type -> thus 'FALSE'
    0 讨论(0)
  • 2020-12-13 19:26

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

    The term "falsy" isn't defined in ECMA-262, it's jargon for where type conversion coerces a value to false. e.g.

    var x = NaN;
    
    if (!x) {
      console.log('x is "falsy"');
    }
    

    The strict equality operator uses the Strict Equality Comparison Algorithm which checks that the arguments are of the same Type, and NaN is Type number, while false is Type boolean, so they evaluated as not equal based on Type, there is no comparison of value.

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

    Because the strict equality comparison algorithm states that NaN !== NaN, hence the isNaN method.

    Using ! coerces the argument to boolean using the abstract ToBoolean method, where !NaN converts to true and !!NaN converts to false, so:

    !!NaN === !!NaN  -->  false === false  -->  true
    

    Note that the abstract equality operator == will coerce the arguments to be of the same Type according to the rules for the Abstract Equality Comparison Algorithm. In this case, NaN is Type number, so false is converted to a number using toNumber which returns 0. And 0 is not equal to NaN so:

    NaN == false  -->  NaN == 0  -->  false
    
    0 讨论(0)
  • 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.<br>
    !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

    0 讨论(0)
  • 2020-12-13 19:31

    This comparison also returns false:

    const x = NaN
    const y = x
    
    console.log(x === y) // false
    
    0 讨论(0)
  • 2020-12-13 19:33
    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.

    0 讨论(0)
  • 2020-12-13 19:39

    === compares both type and value.

    Even though NaN is falsey, === wouldn't be the way to compare it. Something is "falsey" if it evaluates to false in a boolean expression. That isn't the same as being equal to (or equivalent to) false.

    For example, null == false returns false, even though null is falsey. This is not completely intuitive, but that's just how JavaScript handles false/falsey values.

    0 and the blank string ("") are special cases where value equality comparisons against false evaluate to true (i.e. 0 == false and "" == false). However, 0===false and ""===false still returns false.

    NaN is special in that it doesn't have a real value, so comparing it to itself doesn't return true. Essentially, NaN is equal to nothing, not even NaN.

    The only way to reliably compare something to NaN is using isNaN( value ).


    To your second point, !value is a boolean expression. value first undergoes type coercion to a boolean (and remember, NaN is falsey) and then the boolean NOT ! makes it true. As it happens, it's double negated, so !!NaN is the same as boolean false. Of course false === false, so the expression evaluates to true.

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