Is NaN equal to NaN?

前端 未结 8 1891
遇见更好的自我
遇见更好的自我 2020-11-27 05:22
parseFloat(\"NaN\")

returns \"NaN\", but

parseFloat(\"NaN\") == \"NaN\"

returns false. Now, that\'s probably a go

相关标签:
8条回答
  • 2020-11-27 05:43

    NaN is one of the few examples of an object which is not equal to itself. In fact, this very property is used to implement the common bool IsNaN(number) method:

    function isNaN(x)
    { 
        return x != x; 
    }
    
    0 讨论(0)
  • 2020-11-27 05:44
    • When Number (returned by ParseFloat) compares with string string converted to Number
    • NaN is not equal to any other object ( including NaN)

    You get NaN==NaN . It is false by second rule.

    0 讨论(0)
  • 2020-11-27 05:45

    It's a special case, NaN is the only thing in Javascript not equal to itself.

    Although the other answers about strings vs the NaN object are right too.

    0 讨论(0)
  • 2020-11-27 05:46

    In ECMAScript 6 Object.is() is an enhancement of ===. This method accepts two arguments and returns true if the values are equivalent.And the two values are considered equivalent when they are of the same type and have the same value. That's the reason because console.log(Object.is(NaN, NaN))--> TRUE

    0 讨论(0)
  • 2020-11-27 05:49

    When a JavaScript function returns NaN, this is not a literal string but an object property in the global space. You cannot compare it to the string "NaN".

    See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/NaN

    0 讨论(0)
  • 2020-11-27 05:49

    Update 2

    New to ECMAScript 6 is the Object.is() function. This is designed to be a further enhancement of the === check. One of the benefits of this new function is that Object.is(NaN, NaN) will now return true. If you're able to utilize ECMAScript 6, then this would be the most readable and consistent solution for you.

    Original

    The proper way to check this would be:

    isNaN(parseInt(variable))
    

    If whatever you're checking is a NaN, that function will return true. This method is built into the JavaScript spec.

    Using jQuery

    jQuery built in their own isNaN function originally to help counter some discrepancies between browsers, and add some additional checks so their version can be used instead of the one in VanillaJS.

    Update for jQuery

    After jQuery 1.7, they changed this function to $.isNumeric().

    Documentation of the switch

    If you take a look at this Stack Overflow question, you'll find plenty of times where isNaN() returns what would intuitively be considered an "incorrect" answer, but is correct by the spec.

    One of the big reasons to avoid the vanilla isNaN() is that null will return false, making you think it is a number. However, the jQuery function covers a much larger range of intuitive results.

    From their documentation:

    As of jQuery 3.0 $.isNumeric() returns true only if the argument is of type number, or if it's of type string and it can be coerced into finite numbers. In all other cases, it returns false.

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