[javascript] How do you check that a number is NaN in JavaScript?

I’ve only been trying it in Firefox’s JavaScript console, but neither of the following statements return true:

parseFloat('geoff') == NaN;

parseFloat('geoff') == Number.NaN;

This question is related to javascript nan

The answer is


NaN === NaN;        // false
Number.NaN === NaN; // false
isNaN(NaN);         // true
isNaN(Number.NaN);  // true

Equality operator (== and ===) cannot be used to test a value against NaN.

Look at Mozilla Documentation The global NaN property is a value representing Not-A-Numbe

The best way is using 'isNaN()' which is buit-in function to check NaN. All browsers supports the way..


To fix the issue where '1.2geoff' becomes parsed, just use the Number() parser instead.

So rather than this:

parseFloat('1.2geoff'); // => 1.2
isNaN(parseFloat('1.2geoff')); // => false
isNaN(parseFloat('.2geoff')); // => false
isNaN(parseFloat('geoff')); // => true

Do this:

Number('1.2geoff'); // => NaN
isNaN(Number('1.2geoff')); // => true
isNaN(Number('.2geoff')); // => true
isNaN(Number('geoff')); // => true

EDIT: I just noticed another issue from this though... false values (and true as a real boolean) passed into Number() return as 0! In which case... parseFloat works every time instead. So fall back to that:

function definitelyNaN (val) {
    return isNaN(val && val !== true ? Number(val) : parseFloat(val));
}

And that covers seemingly everything. I benchmarked it at 90% slower than lodash's _.isNaN but then that one doesn't cover all the NaN's:

http://jsperf.com/own-isnan-vs-underscore-lodash-isnan

Just to be clear, mine takes care of the human literal interpretation of something that is "Not a Number" and lodash's takes care of the computer literal interpretation of checking if something is "NaN".


As of ES6, Object.is(..) is a new utility that can be used to test two values for absolute equality:

var a = 3 / 'bar';
Object.is(a, NaN); // true

It seems that isNaN() is not supported in Node.js out of the box.
I worked around with

var value = 1;
if (parseFloat(stringValue)+"" !== "NaN") value = parseFloat(stringValue);

I use underscore's isNaN function because in JavaScript:

isNaN(undefined) 
-> true

At the least, be aware of that gotcha.


According to IEEE 754, all relationships involving NaN evaluate as false except !=. Thus, for example, (A >= B) = false and (A <= B) = false if A or B or both is/are NaN.


While @chiborg 's answer IS correct, there is more to it that should be noted:

parseFloat('1.2geoff'); // => 1.2
isNaN(parseFloat('1.2geoff')); // => false
isNaN(parseFloat('.2geoff')); // => false
isNaN(parseFloat('geoff')); // => true

Point being, if you're using this method for validation of input, the result will be rather liberal.

So, yes you can use parseFloat(string) (or in the case of full numbers parseInt(string, radix)' and then subsequently wrap that with isNaN(), but be aware of the gotcha with numbers intertwined with additional non-numeric characters.


Use this code:

isNaN('geoff');

See isNaN() docs on MDN.

alert ( isNaN('abcd'));  // alerts true
alert ( isNaN('2.0'));  // alerts false
alert ( isNaN(2.0));  // alerts false

_x000D_
_x000D_
function isNotANumber(n) {_x000D_
  if (typeof n !== 'number') {_x000D_
    return true;_x000D_
  } _x000D_
  return n !== n;_x000D_
}
_x000D_
_x000D_
_x000D_


I've created this little function that works like a charm. Instead of checking for NaN which seems to be counter intuitive, you check for a number. I'm pretty sure I am not the first to do it this way, but I thought i'd share.

function isNum(val){
    var absVal = Math.abs(val);
    var retval = false;
    if((absVal-absVal) == 0){
        retval = true
    }

    return retval;
}

marksyzm's answer works well, but it does not return false for Infinity as Infinity is techinicly not a number.

i came up with a isNumber function that will check if it is a number.

_x000D_
_x000D_
function isNumber(i) {_x000D_
    return !isNaN(i && i !== true ? Number(i) : parseFloat(i)) && [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY].indexOf(i) === -1;_x000D_
}_x000D_
_x000D_
console.log(isNumber(Infinity));_x000D_
console.log(isNumber("asdf"));_x000D_
console.log(isNumber(1.4));_x000D_
console.log(isNumber(NaN));_x000D_
console.log(isNumber(Number.MAX_VALUE));_x000D_
console.log(isNumber("1.68"));
_x000D_
_x000D_
_x000D_

UPDATE: i noticed that this code fails for some parameters, so i made it better.

_x000D_
_x000D_
function isNumber(i) {//function for checking if parameter is number_x000D_
if(!arguments.length) {_x000D_
throw new SyntaxError("not enough arguments.");_x000D_
 } else if(arguments.length > 1) {_x000D_
throw new SyntaxError("too many arguments.");_x000D_
 } else if([Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY].indexOf(i) !== -1) {_x000D_
throw new RangeError("number cannot be \xB1infinity.");_x000D_
 } else if(typeof i === "object" && !(i instanceof RegExp) && !(i instanceof Number) && !(i === null)) {_x000D_
throw new TypeError("parameter cannot be object/array.");_x000D_
 } else if(i instanceof RegExp) {_x000D_
throw new TypeError("parameter cannot be RegExp.");_x000D_
 } else if(i == null || i === undefined) {_x000D_
throw new ReferenceError("parameter is null or undefined.");_x000D_
 } else {_x000D_
return !isNaN(i && i !== true ? Number(i) : parseFloat(i)) && (i === i);_x000D_
 }_x000D_
}_x000D_
console.log(isNumber(Infinity));_x000D_
console.log(isNumber(this));_x000D_
console.log(isNumber(/./ig));_x000D_
console.log(isNumber(null));
_x000D_
_x000D_
_x000D_


NaN in JavaScript stands for "Not A Number", although its type is actually number.

typeof(NaN) // "number"

To check if a variable is of value NaN, we cannot simply use function isNaN(), because isNaN() has the following issue, see below:

var myVar = "A";
isNaN(myVar) // true, although "A" is not really of value NaN

What really happens here is that myVar is implicitly coerced to a number:

var myVar = "A";
isNaN(Number(myVar)) // true. Number(myVar) is NaN here in fact

It actually makes sense, because "A" is actually not a number. But what we really want to check is if myVar is exactly of value NaN.

So isNaN() cannot help. Then what should we do instead?

In the light that NaN is the only JavaScript value that is treated unequal to itself, so we can check for its equality to itself using !==

var myVar; // undefined
myVar !== myVar // false

var myVar = "A";
myVar !== myVar // false

var myVar = NaN
myVar !== myVar // true

So to conclude, if it is true that a variable !== itself, then this variable is exactly of value NaN:

function isOfValueNaN(v) {
    return v !== v;
}

var myVar = "A";
isNaN(myVar); // true
isOfValueNaN(myVar); // false

So I see several responses to this,

But I just use:

function isNaN(x){
     return x == x && typeof x == 'number';
}

alert("1234567890.".indexOf(String.fromCharCode(mycharacter))>-1);

This is not elegant. but after trying isNAN() I arrived at this solution which is another alternative. In this example I also allowed '.' because I am masking for float. You could also reverse this to make sure no numbers are used.

("1234567890".indexOf(String.fromCharCode(mycharacter))==-1)

This is a single character evaluation but you could also loop through a string to check for any numbers.


NaN is a special value that can't be tested like that. An interesting thing I just wanted to share is this

var nanValue = NaN;
if(nanValue !== nanValue) // Returns true!
    alert('nanValue is NaN');

This returns true only for NaN values and Is a safe way of testing. Should definitely be wrapped in a function or atleast commented, because It doesnt make much sense obviously to test if the same variable is not equal to each other, hehe.


The exact way to check is:

//takes care of boolen, undefined and empty

isNaN(x) || typeof(x) ==='boolean' || typeof(x) !=='undefined' || x!=='' ? 'is really a nan' : 'is a number'

The rule is:

NaN != NaN

The problem of isNaN() function is that it may return unexpected result in some cases:

isNaN('Hello')      //true
isNaN('2005/12/12') //true
isNaN(undefined)    //true
isNaN('NaN')        //true
isNaN(NaN)          //true
isNaN(0 / 0)        //true

A better way to check if the value is really NaN is:

function is_nan(value) {
    return value != value
}

is_nan(parseFloat("geoff"))

I just came across this technique in the book Effective JavaScript that is pretty simple:

Since NaN is the only JavaScript value that is treated as unequal to itself, you can always test if a value is NaN by checking it for equality to itself:

var a = NaN;
a !== a; // true 

var b = "foo";
b !== b; // false 

var c = undefined; 
c !== c; // false

var d = {};
d !== d; // false

var e = { valueOf: "foo" }; 
e !== e; // false

Didn't realize this until @allsyed commented, but this is in the ECMA spec: https://tc39.github.io/ecma262/#sec-isnan-number


I just want to share another alternative, it's not necessarily better than others here, but I think it's worth looking at:

function customIsNaN(x) { return (typeof x == 'number' && x != 0 && !x); }

The logic behind this is that every number except 0 and NaN are cast to true.

I've done a quick test, and it performs as good as Number.isNaN and as checking against itself for false. All three perform better than isNan

The results

customIsNaN(NaN);            // true
customIsNaN(0/0);            // true
customIsNaN(+new Date('?')); // true

customIsNaN(0);          // false
customIsNaN(false);      // false
customIsNaN(null);       // false
customIsNaN(undefined);  // false
customIsNaN({});         // false
customIsNaN('');         // false

May become useful if you want to avoid the broken isNaN function.


Maybe also this:

function isNaNCustom(value){
    return value.toString() === 'NaN' && 
           typeof value !== 'string' && 
           typeof value === 'number'
}

As far as a value of type Number is to be tested whether it is a NaN or not, the global function isNaN will do the work

isNaN(any-Number);

For a generic approach which works for all the types in JS, we can use any of the following:

For ECMAScript-5 Users:

#1
if(x !== x) {
    console.info('x is NaN.');
}
else {
    console.info('x is NOT a NaN.');
}

For people using ECMAScript-6:

#2
Number.isNaN(x);

And For consistency purpose across ECMAScript 5 & 6 both, we can also use this polyfill for Number.isNan

#3
//Polyfill from MDN
Number.isNaN = Number.isNaN || function(value) {
    return typeof value === "number" && isNaN(value);
}
// Or
Number.isNaN = Number.isNaN || function(value) {     
    return value !== value;
}

please check This Answer for more details.


You should use the global isNaN(value) function call, because:

  • It is supported cross-browser
  • See isNaN for documentation

Examples:

 isNaN('geoff'); // true
 isNaN('3'); // false

I hope this will help you.


Found another way, just for fun.

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

Simply convert the result to String and compare with 'NaN'.

var val = Number("test");
if(String(val) === 'NaN') {
   console.log("true");
}

If your environment supports ECMAScript 2015, then you might want to use Number.isNaN to make sure that the value is really NaN.

The problem with isNaN is, if you use that with non-numeric data there are few confusing rules (as per MDN) are applied. For example,

isNaN(NaN);       // true
isNaN(undefined); // true
isNaN({});        // true

So, in ECMA Script 2015 supported environments, you might want to use

Number.isNaN(parseFloat('geoff'))

Another solution is mentioned in MDN's parseFloat page

It provides a filter function to do strict parsing

var filterFloat = function (value) {
    if(/^(\-|\+)?([0-9]+(\.[0-9]+)?|Infinity)$/
      .test(value))
      return Number(value);
  return NaN;
}


console.log(filterFloat('421'));               // 421
console.log(filterFloat('-421'));              // -421
console.log(filterFloat('+421'));              // 421
console.log(filterFloat('Infinity'));          // Infinity
console.log(filterFloat('1.61803398875'));     // 1.61803398875
console.log(filterFloat('421e+0'));            // NaN
console.log(filterFloat('421hop'));            // NaN
console.log(filterFloat('hop1.61803398875'));  // NaN

And then you can use isNaN to check if it is NaN


I wrote this answer to another question on StackOverflow where another checks when NaN == null but then it was marked as duplicate so I don't want to waste my job.

Look at Mozilla Developer Network about NaN.


Short answer

Just use distance || 0 when you want to be sure you value is a proper number or isNaN() to check it.

Long answer

The NaN (Not-a-Number) is a weirdo Global Object in javascript frequently returned when some mathematical operation failed.

You wanted to check if NaN == null which results false. Hovewer even NaN == NaN results with false.

A Simple way to find out if variable is NaN is an global function isNaN().

Another is x !== x which is only true when x is NaN. (thanks for remind to @raphael-schweikert)

But why the short answer worked?

Let's find out.

When you call NaN == false the result is false, same with NaN == true.

Somewhere in specifications JavaScript has an record with always false values, which includes:

  • NaN - Not-a-Number
  • "" - empty string
  • false - a boolean false
  • null - null object
  • undefined - undefined variables
  • 0 - numerical 0, including +0 and -0

Simple Solution!

REALLY super simple! Here! Have this method!

function isReallyNaN(a) { return a !== a; };

Use as simple as:

if (!isReallyNaN(value)) { return doingStuff; }

See performance test here using this func vs selected answer

Also: See below 1st example for a couple alternate implementations.


Example:

_x000D_
_x000D_
function isReallyNaN(a) { return a !== a; };_x000D_
_x000D_
var example = {_x000D_
    'NaN': NaN,_x000D_
    'an empty Objet': {},_x000D_
    'a parse to NaN': parseFloat('$5.32'),_x000D_
    'a non-empty Objet': { a: 1, b: 2 },_x000D_
    'an empty Array': [],_x000D_
    'a semi-passed parse': parseInt('5a5'),_x000D_
    'a non-empty Array': [ 'a', 'b', 'c' ],_x000D_
    'Math to NaN': Math.log(-1),_x000D_
    'an undefined object': undefined_x000D_
  }_x000D_
_x000D_
for (x in example) {_x000D_
    var answer = isReallyNaN(example[x]),_x000D_
        strAnswer = answer.toString();_x000D_
    $("table").append($("<tr />", { "class": strAnswer }).append($("<th />", {_x000D_
        html: x_x000D_
    }), $("<td />", {_x000D_
        html: strAnswer_x000D_
    })))_x000D_
};
_x000D_
table { border-collapse: collapse; }_x000D_
th, td { border: 1px solid; padding: 2px 5px; }_x000D_
.true { color: red; }_x000D_
.false { color: green; }
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>_x000D_
<table></table>
_x000D_
_x000D_
_x000D_

There are a couple alternate paths you take for implementaion, if you don't want to use an alternately named method, and would like to ensure it's more globally available. Warning These solutions involve altering native objects, and may not be your best solution. Always use caution and be aware that other Libraries you might use may depend on native code or similar alterations.

Alternate Implementation 1: Replace Native isNaN method.

//  Extremely simple. Just simply write the method.
window.isNaN = function(a) { return a !==a; }

Alternate Implementation 2: Append to Number Object
*Suggested as it is also a poly-fill for ECMA 5 to 6

Number['isNaN'] || (Number.isNaN = function(a) { return a !== a });
//  Use as simple as
Number.isNaN(NaN)

Alternate solution test if empty

A simple window method I wrote that test if object is Empty. It's a little different in that it doesn't give if item is "exactly" NaN, but I figured I'd throw this up as it may also be useful when looking for empty items.

/** isEmpty(varried)
 *  Simple method for testing if item is "empty"
 **/
;(function() {
   function isEmpty(a) { return (!a || 0 >= a) || ("object" == typeof a && /\{\}|\[(null(,)*)*\]/.test(JSON.stringify(a))); };
   window.hasOwnProperty("empty")||(window.empty=isEmpty);
})();

Example:

_x000D_
_x000D_
;(function() {_x000D_
   function isEmpty(a) { return !a || void 0 === a || a !== a || 0 >= a || "object" == typeof a && /\{\}|\[(null(,)*)*\]/.test(JSON.stringify(a)); };_x000D_
   window.hasOwnProperty("empty")||(window.empty=isEmpty);_x000D_
})();_x000D_
_x000D_
var example = {_x000D_
    'NaN': NaN,_x000D_
    'an empty Objet': {},_x000D_
    'a parse to NaN': parseFloat('$5.32'),_x000D_
    'a non-empty Objet': { a: 1, b: 2 },_x000D_
    'an empty Array': new Array(),_x000D_
    'an empty Array w/ 9 len': new Array(9),_x000D_
    'a semi-passed parse': parseInt('5a5'),_x000D_
    'a non-empty Array': [ 'a', 'b', 'c' ],_x000D_
    'Math to NaN': Math.log(-1),_x000D_
    'an undefined object': undefined_x000D_
  }_x000D_
_x000D_
for (x in example) {_x000D_
 var answer = empty(example[x]),_x000D_
  strAnswer = answer.toString();_x000D_
 $("#t1").append(_x000D_
  $("<tr />", { "class": strAnswer }).append(_x000D_
   $("<th />", { html: x }),_x000D_
   $("<td />", { html: strAnswer.toUpperCase() })_x000D_
  )_x000D_
 )_x000D_
};_x000D_
_x000D_
_x000D_
function isReallyNaN(a) { return a !== a; };_x000D_
for(x in example){var answer=isReallyNaN(example[x]),strAnswer=answer.toString();$("#t2").append($("<tr />",{"class":strAnswer}).append($("<th />",{html:x}),$("<td />",{html:strAnswer.toUpperCase()})))};
_x000D_
table { border-collapse: collapse; float: left; }_x000D_
th, td { border: 1px solid; padding: 2px 5px; }_x000D_
.true { color: red; }_x000D_
.false { color: green; }
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>_x000D_
<table id="t1"><thead><tr><th colspan="2">isEmpty()</th></tr></thead><thead><tr><th>Value Type</th><th>Bool Return</th></tr></thead></table>_x000D_
<table id="t2"><thead><tr><th colspan="2">isReallyNaN()</th></tr></thead><thead><tr><th>Value Type</th><th>Bool Return</th></tr></thead></table>
_x000D_
_x000D_
_x000D_


Extremely Deep Check If Is Empty

This last one goes a bit deep, even checking if an Object is full of blank Objects. I'm sure it has room for improvement and possible pits, but so far, it appears to catch most everything.

_x000D_
_x000D_
function isEmpty(a) {_x000D_
 if (!a || 0 >= a) return !0;_x000D_
 if ("object" == typeof a) {_x000D_
  var b = JSON.stringify(a).replace(/"[^"]*":(0|"0*"|false|null|\{\}|\[(null(,)?)*\]),?/g, '').replace(/"[^"]*":\{\},?/g, '');_x000D_
  if ( /^$|\{\}|\[\]/.test(b) ) return !0;_x000D_
  else if (a instanceof Array)  {_x000D_
   b = b.replace(/(0|"0*"|false|null|\{\}|\[(null(,)?)*\]),?/g, '');_x000D_
   if ( /^$|\{\}|\[\]/.test(b) ) return !0;_x000D_
  }_x000D_
 }_x000D_
 return false;_x000D_
}_x000D_
window.hasOwnProperty("empty")||(window.empty=isEmpty);_x000D_
_x000D_
var example = {_x000D_
    'NaN': NaN,_x000D_
    'an empty Objet': {},_x000D_
    'a parse to NaN': parseFloat('$5.32'),_x000D_
    'a non-empty Objet': { a: 1, b: 2 },_x000D_
    'an empty Array': new Array(),_x000D_
    'an empty Array w/ 9 len': new Array(9),_x000D_
    'a semi-passed parse': parseInt('5a5'),_x000D_
    'a non-empty Array': [ 'a', 'b', 'c' ],_x000D_
    'Math to NaN': Math.log(-1),_x000D_
    'an undefined object': undefined,_x000D_
    'Object Full of Empty Items': { 1: '', 2: [], 3: {}, 4: false, 5:new Array(3), 6: NaN, 7: null, 8: void 0, 9: 0, 10: '0', 11: { 6: NaN, 7: null, 8: void 0 } },_x000D_
    'Array Full of Empty Items': ["",[],{},false,[null,null,null],null,null,null,0,"0",{"6":null,"7":null}]_x000D_
  }_x000D_
_x000D_
for (x in example) {_x000D_
 var answer = empty(example[x]),_x000D_
  strAnswer = answer.toString();_x000D_
 $("#t1").append(_x000D_
  $("<tr />", { "class": strAnswer }).append(_x000D_
   $("<th />", { html: x }),_x000D_
   $("<td />", { html: strAnswer.toUpperCase() })_x000D_
  )_x000D_
 )_x000D_
};_x000D_
_x000D_
_x000D_
function isReallyNaN(a) { return a !== a; };_x000D_
for(x in example){var answer=isReallyNaN(example[x]),strAnswer=answer.toString();$("#t2").append($("<tr />",{"class":strAnswer}).append($("<th />",{html:x}),$("<td />",{html:strAnswer.toUpperCase()})))};
_x000D_
table { border-collapse: collapse; float: left; }_x000D_
th, td { border: 1px solid; padding: 2px 5px; }_x000D_
.true { color: red; }_x000D_
.false { color: green; }
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>_x000D_
<table id="t1"><thead><tr><th colspan="2">isEmpty()</th></tr></thead><thead><tr><th>Value Type</th><th>Bool Return</th></tr></thead></table>_x000D_
<table id="t2"><thead><tr><th colspan="2">isReallyNaN()</th></tr></thead><thead><tr><th>Value Type</th><th>Bool Return</th></tr></thead></table>
_x000D_
_x000D_
_x000D_


Is (NaN >= 0) ?...... "I don't Know".

function IsNotNumber( i ){
    if( i >= 0 ){ return false; }
    if( i <= 0 ){ return false; }
    return true;
}

Conditions only execute if TRUE.

Not on FALSE.

Not on "I Don't Know".