Is NaN equal to NaN?
Asked Answered
B

8

55
parseFloat("NaN")

returns "NaN", but

parseFloat("NaN") == "NaN"

returns false. Now, that's probably a good thing that it does return false, but I don't understand how this is so. Did the JavaScript creators just make this a special case? Because otherwise I can't understand how this returns false.

Baptlsta answered 8/8, 2011 at 0:15 Comment(5)
A NaN is never equal to itself, by definition. It works this way in any language.Turn
Note my answer below as a precise method for checking this has been added with ECMAScript 6.Lash
The event best part is that parseFloat("A") == parseFloat("A") return falseCelebes
@MariuszJamro That's because parseFloat("A") returns NaN on each side. As the answers have covered, NaN is not even equal to itself.Lash
parseFloat("NaN") == NaN <-- this is the more interesting question (without the quotes). Still no. NaN is toxic. Use isNaN().Scalpel
N
38

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

Neediness answered 8/8, 2011 at 0:17 Comment(1)
NaN is not an object; it is a special primitive value. (Which happens to be the value of a read-only property of the global object).Transaction
L
41

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.

Lash answered 27/9, 2012 at 19:5 Comment(3)
This has nothing to do with jQuery. What?Kazantzakis
Just because jQuery changed the function they're using doesn't mean you have to.La
parseInt() and parseFloat() have the fairly significant problem of accepting "garbage" characters that appear in a string after a valid number.Platypus
N
38

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

Neediness answered 8/8, 2011 at 0:17 Comment(1)
NaN is not an object; it is a special primitive value. (Which happens to be the value of a read-only property of the global object).Transaction
O
37

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.

Odoriferous answered 8/8, 2011 at 0:17 Comment(2)
Why then does "NaN" == "NaN" return true? It probably wouldn't make sense for that to return false, but...Baptlsta
@Baptlsta "NaN" == "NaN" is comparing two (equal) strings.Kumiss
H
12

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; 
}
Hallmark answered 22/5, 2014 at 21:29 Comment(4)
that's funny looking javascriptEtz
Changed the example from C# to JavaScript.Hallmark
"few examples"? is there another one? If so this implementation fails on those other examples.Baby
NaN is not an object at all, it is a constant with type number.Platypus
A
9

isNaN works for all values that aren't numbers

isNaN('foo') == true // a string is indeed not a number
isNaN(NaN) == true
isNaN(12) == false
isNaN([1,2,3]) == true // an array is also not a number

If, however you want to check for NaN specifically, or avoid type coercion;
you can use Number.isNaN instead

Number.isNaN('foo') == false
Number.isNaN(NaN) == true
Number.isNaN(12) == false
Number.isNaN([1,2,3]) == false
Abeabeam answered 1/10, 2015 at 9:42 Comment(0)
S
8
  • 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.

Shurlocke answered 8/8, 2011 at 0:23 Comment(0)
A
1

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

Anatol answered 5/4, 2017 at 15:55 Comment(0)
T
0

I'm working with Google Apps Script and so I'm stuck with ECMA 5. Similar to Electric Coffee's answer, here's what I was able to figure out that seems to give a sure answer as to whether or not a value is actually NaN, not if a value is NaN but if it is actually NaN itself:

function isThisNaN(x)
{ 
    return isNaN(x) && Object.prototype.toString.call(x) === '[object Number]'; 
}
console.log(isThisNaN(NaN)); // true

lol Just funny to me that Object.prototype.toString.call(NaN) equals '[object Number]'. My newbie brain tells me that NaN is "Not a Number" but sadly it's just not that simple.

EDIT: I guess I should have said how I ended up at this article. I went with the idea that surely a string that doesn't contain a number wouldn't be treated as a number... well, I ended up finding this out:

isNaN('a'); // true
isNaN(' '); // false

so even though ' ' is a non-numerical string it apparently gets coaxed into a number (0).

console.log(Number(' ')); // 0.0

however...

console.log( 0  ? true : false); // false
console.log(' ' ? true : false); // true

After reading more I do understand it a bit better but wow what a mental crapstorm for a newbie lol

Tallbott answered 17/12, 2017 at 6:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.