Comparing NaN Values ​​for Equality in Javascript

I need to compare two numeric values ​​for equality in Javascript. Values ​​may be NaN . I came up with this code:

 if (val1 == val2 || isNaN(val1) && isNaN(val2)) ... 

which works fine, but it looks bloated. I would like to make it more concise. Any ideas?

+59
javascript equality comparison nan
Jan 22 '12 at 22:40
source share
12 answers
 if(val1 == val2 || (isNaN(val1) && isNaN(val2))) 

Nothing will improve. Just add parentheses for everyone to understand.

+51
Jan 22 2018-12-22T00:
source share
β€” -

Avoid isNaN . His behavior is misleading:

 isNaN(undefined) // true 

_.isNaN (from Underscore.js ) is an elegant function that behaves as expected:

 // Is the given value 'NaN'? // // 'NaN' is the only value for which '===' is not reflexive. _.isNaN = function(obj) { return obj !== obj; }; _.isNaN(undefined) // false _.isNaN(0/0) // true 
+24
Jan 22 2018-12-22T00:
source share

if ( val1 === val2 )

If one or both are NaN , it will be evaluated as false.

In addition, NaN !== NaN

+5
Jan 22 2018-12-22T00:
source share

NaN never matches itself regardless of the comparison method, so the only more concise solution for your problem that I can think of would be to create a function call with a descriptive name for this rather special comparison and use this comparison instead in your code.

It will also have the advantage of localizing changes to the algorithm that day when you decide that undefined should be equal to undefined.

+5
Jan 22 2018-12-22T00:
source share

As long as you know that these two variables are numeric, you can try:

 if (val1 + '' == val2 + '') 

It turns two values ​​into strings. Funny answer, but it should work. :)

+5
Jan 22 '12 at 22:45
source share

Try using Object.is() , it determines if two values ​​are the same. Two values ​​are the same if one of the following is true:

  • both undefined
  • both null
  • both true or both false
  • both strings of the same length with the same characters in the same order
  • as one and the same object
  • both numbers and
    • both +0
    • both -0
    • both NaN
    • or both nonzero and both non NaN and both have the same value

e.g. Object.is(NaN, NaN) => true

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is.

+5
Jan 17 '18 at 11:48
source share

What about the Number.isNaN () function? I believe that this should be used whenever possible.

 > NaN === NaN false > Number.isNaN Ζ’ isNaN() { [native code] } > Number.isNaN() === Number.isNaN() true 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN

+2
Jun 16 '15 at 21:29
source share

For numerical cases, the solution is fine, but for expanding it to work with other data types, my suggestion will be as follows:

 if(val1 === val2 || (val1 !== val1 && val2 !== val2)) 

The cause of the global isNaN is erroneous. This will give you incorrect results in scenarios like

 isNaN(undefined); // true isNaN({}); // true isNaN("lorem ipsum"); // true 

I have posted a comprehensive answer here that also covers NaN comparisons for equality.

How to check if JavaScript variable is NaN

+2
Aug 21 '15 at 10:32
source share

I created this answer after considering proposals from ThiefMaster, Esailija, Joachim Isaksson and davidchambers. Could this be improved?

 // Determines if two numeric values are equal. // Also returns true when both parameters are NaN. function areEqualNumeric(val1, val2) { return val1 === val2 || (val1 !== val1 && val2 !== val2); } 
+1
May 3, '13 at 2:35 pm
source share

Why not an if statement like this?

 if (isNaN(x) == true){ alert("This is not a number."); } 
-one
Jul 06 '15 at 17:55
source share

Comparing equality with NaN always results in False .

We can go for the javascript isNaN() function to check for equality with NaN. Example:

 1. isNaN(123) //false 2. var array = [3, NaN]; for(var i = 0 ; i< array.length; i++){ if(isNaN(array[i])){ console.log("True ---- Values of " + i); } else { console.log("false ---- Values of " + i); } } 

Results:

false ---- Values ​​0

True ---- Values ​​1

-one
Apr 13 '16 at 9:35
source share

Found another way using the Array.prototype.includes MDN link . Apparently, [NaN] .includes (NaN) returns true for NaN.

 function IsActuallyNaN(obj) { return [obj].includes(NaN); } 

Or we can go with the davidchambers solution, which is much simpler.

 function IsActuallyNaN2(obj) { return obj !== obj; } 
-one
May 19 '17 at 4:35 a.m.
source share



All Articles