How do you check that a number is NaN in JavaScript?
Asked Answered
A

33

549

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;
Ardisardisj answered 16/4, 2010 at 10:56 Comment(9)
worth reading if your actual goal is to check numbers: #18582Jacindajacinta
designpepper.com/blog/drips/…Madore
@Gothdo: sure, but I did ask how to check that a number is NaN, as opposed to any value.Ardisardisj
@PaulD.Waite Yes, but lots of people come here when Googling something like "how to check if a variable is nan in javascript".Ahouh
PaulD.Waite I agree with you, but I have the same concern as @Gothdo has. Many people land on this question with the intention as "How to check if a value in JS is NaN". You can check mjohnsonengr's comment on the same answer. BTW, I too have flagged This question for moderators attention.Calculus
@dopeddude: yeah I suggested this question should be closed as a duplicate of 30314447 when I flagged it. That might help.Ardisardisj
@Gothdo: those people need to learn to read things carefully.Ardisardisj
NaN is not equal to NaN for some reason Check with NaN == NaN and it returns false The thing is, there is nothing you can compare with NaN which will make it say it's equalAnnual
in ES6 (aka ECMAScript 2015), use Number.isNaN, check this answer and MDNUnderwent
F
732

Try this code:

isNaN(parseFloat("geoff"))

For checking whether any value is NaN, instead of just numbers, see here: How do you test for NaN in Javascript?

Faery answered 16/4, 2010 at 10:58 Comment(7)
Is there a need for paseFloat?Bessette
If you want an empty string to be interpreted as NaN, then yes. (I’m not saying you always would.)Ardisardisj
If i were you i would assign it to a variable and , use condition variable !== variable . As it is laid out in specs tc39.github.io/ecma262/#sec-isnan-numberIman
isNaN(parseFloat("geoff")) // true isNaN(parseFloat("10geo")) // false How's this useful?Helle
This could also be useful as to try out w3schools.com/jsref/jsref_isnan.aspUnaccountedfor
@PaulD.Waite and why it should be parseFloat instead of parseIntEarldom
parseInt(".3") is NaN but parseFloat(".3") is a number.Bioastronautics
P
180

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.es/ecma262/multipage/global-object.html#sec-isnan-number

Pusillanimous answered 7/6, 2013 at 16:5 Comment(13)
Sure: also noted in @MrAzulay’s answer.Ardisardisj
Effective but counterintuitive. I wouldn't want to maintain this kind of code.Barone
It's not the code that's counterintuitive, it's NaN.Pusillanimous
This is a bad idea. What if some new object introduced in some future version of JavaScript also doesn't equal itself? Or if NaN is improved in the future and this bug goes away?Bursitis
@DanM It's not a bug, it's the standard IEE754 behavior of NaN. It's how it's implemented in CPUs, and it's the expected behavior in every programming language in the world that uses floats.Hemidemisemiquaver
@Hemidemisemiquaver Duly noted! That's actually really interesting; I always thought NaN inequality weirdness was a Javascript thing.Bursitis
Must it be done with the strict equality operator !== or can it be done with the regular !=?Faircloth
Best to always use === and !==Pusillanimous
A bit weird looking at it. Would you suggest using a function to abstract it? Or would NaN do something bad there too?Norikonorina
@Norikonorina There already is a function: isNaN. And NaN is not doing "something bad" here or elsewhere.Birdwell
NaN === Nan; // false. This is a known bug in javascript. isNan is a new method.Stargell
@Stargell - It's not a bug, it's a feature.NaN isn't a value, it's a signal that something went wrong. Moreover, it's a signal that mathematic operations, including ===, won't produce sane results. +"foo" === +undefined shouldn't evaluate to true, and +"foo" === +"bar" would be arguably worse.Maurinemaurise
Since NaN is the only JavaScript value that is treated as unequal to itself - excellent @PusillanimousMydriatic
B
64

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
Bessette answered 16/4, 2010 at 10:59 Comment(2)
Maybe it's just me, but the idea of NaN gets confusing when var value = 0/0; and var value2= String("123 131"); create NaN values and something like this var value3 = "abcd"; is also a NaN value.Jackinthepulpit
@NickPineda Why?Bluegreen
C
52

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.

Calculus answered 21/8, 2015 at 10:11 Comment(6)
Thanks for not just using "isNan()" but rather providing a completely detailed answer. This is EXACTLY what I was looking for when I found this question on Google.Casuistry
".... was giving wrong results for the above cases" I wouldn't say wrong, just different, because the global isNaN converts the argument to a number value first.Olcott
@FelixKling Please check the updated answer. Thanks.Calculus
isNaN("lorem ipsum"); //true with ES5 was blowing my mindJackinthepulpit
@NickPineda The feeling is so mutual, but finally MDN could explain it this way "When the argument to the isNaN function is not of type Number, the value is first coerced to a Number. The resulting value is then tested to determine whether it is NaN."Calculus
Basic difference is that one checks for "not a number" other one checks if it's not the special NaN, Number.isNaN("aa") therefore returns false and isNaN("aa") returns true.Aleurone
O
20

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.

Obolus answered 16/4, 2010 at 10:57 Comment(1)
Yeah, isNaN('') //false but parseInt('') //NaN. The same can be said for null.Brothel
A
19

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
Arvizu answered 28/12, 2015 at 3:26 Comment(2)
NaN doc at MDNPalsgrave
The two keetss below are different. var a = 0/0; console.log(a === NaN); // false console.log(Object.is(a, NaN)); // trueKt
A
17

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.

Avan answered 8/1, 2013 at 17:27 Comment(2)
That's not a perfectly reliable way. As per the specs. variable !== variable is most reliable way to check for NaNIman
See my answer above on this ^Pusillanimous
I
14

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
Illinium answered 9/9, 2017 at 8:40 Comment(0)
S
10

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".

Sherronsherry answered 17/3, 2014 at 10:57 Comment(0)
G
9

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"))
Ger answered 16/2, 2019 at 6:26 Comment(0)
V
8

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.

Vuillard answered 24/8, 2013 at 23:8 Comment(2)
Interesting. Any way to counter this and detect that "1.2geoff" is not really a valid number string?Moonmoonbeam
Note that this happens only when the string begins with a number. parseFloat('test1.2') will return NaN.Adham
L
6

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:

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

var example = {
    'NaN': NaN,
    'an empty Objet': {},
    'a parse to NaN': parseFloat('$5.32'),
    'a non-empty Objet': { a: 1, b: 2 },
    'an empty Array': [],
    'a semi-passed parse': parseInt('5a5'),
    'a non-empty Array': [ 'a', 'b', 'c' ],
    'Math to NaN': Math.log(-1),
    'an undefined object': undefined
  }

for (x in example) {
    var answer = isReallyNaN(example[x]),
        strAnswer = answer.toString();
    $("table").append($("<tr />", { "class": strAnswer }).append($("<th />", {
        html: x
    }), $("<td />", {
        html: strAnswer
    })))
};
table { border-collapse: collapse; }
th, td { border: 1px solid; padding: 2px 5px; }
.true { color: red; }
.false { color: green; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table></table>

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:

;(function() {
   function isEmpty(a) { return !a || void 0 === a || a !== a || 0 >= a || "object" == typeof a && /\{\}|\[(null(,)*)*\]/.test(JSON.stringify(a)); };
   window.hasOwnProperty("empty")||(window.empty=isEmpty);
})();

var example = {
    'NaN': NaN,
    'an empty Objet': {},
    'a parse to NaN': parseFloat('$5.32'),
    'a non-empty Objet': { a: 1, b: 2 },
    'an empty Array': new Array(),
    'an empty Array w/ 9 len': new Array(9),
    'a semi-passed parse': parseInt('5a5'),
    'a non-empty Array': [ 'a', 'b', 'c' ],
    'Math to NaN': Math.log(-1),
    'an undefined object': undefined
  }

for (x in example) {
	var answer = empty(example[x]),
		strAnswer = answer.toString();
	$("#t1").append(
		$("<tr />", { "class": strAnswer }).append(
			$("<th />", { html: x }),
			$("<td />", { html: strAnswer.toUpperCase() })
		)
	)
};


function isReallyNaN(a) { return a !== a; };
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()})))};
table { border-collapse: collapse; float: left; }
th, td { border: 1px solid; padding: 2px 5px; }
.true { color: red; }
.false { color: green; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<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>
<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>

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.

function isEmpty(a) {
	if (!a || 0 >= a) return !0;
	if ("object" == typeof a) {
		var b = JSON.stringify(a).replace(/"[^"]*":(0|"0*"|false|null|\{\}|\[(null(,)?)*\]),?/g, '').replace(/"[^"]*":\{\},?/g, '');
		if ( /^$|\{\}|\[\]/.test(b) ) return !0;
		else if (a instanceof Array)  {
			b = b.replace(/(0|"0*"|false|null|\{\}|\[(null(,)?)*\]),?/g, '');
			if ( /^$|\{\}|\[\]/.test(b) ) return !0;
		}
	}
	return false;
}
window.hasOwnProperty("empty")||(window.empty=isEmpty);

var example = {
    'NaN': NaN,
    'an empty Objet': {},
    'a parse to NaN': parseFloat('$5.32'),
    'a non-empty Objet': { a: 1, b: 2 },
    'an empty Array': new Array(),
    'an empty Array w/ 9 len': new Array(9),
    'a semi-passed parse': parseInt('5a5'),
    'a non-empty Array': [ 'a', 'b', 'c' ],
    'Math to NaN': Math.log(-1),
    'an undefined object': undefined,
    '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 } },
    'Array Full of Empty Items': ["",[],{},false,[null,null,null],null,null,null,0,"0",{"6":null,"7":null}]
  }

for (x in example) {
	var answer = empty(example[x]),
		strAnswer = answer.toString();
	$("#t1").append(
		$("<tr />", { "class": strAnswer }).append(
			$("<th />", { html: x }),
			$("<td />", { html: strAnswer.toUpperCase() })
		)
	)
};


function isReallyNaN(a) { return a !== a; };
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()})))};
table { border-collapse: collapse; float: left; }
th, td { border: 1px solid; padding: 2px 5px; }
.true { color: red; }
.false { color: green; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<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>
<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>
Lychnis answered 21/1, 2015 at 12:53 Comment(5)
@PaulD.Waite maybe its this app, but I dont see an accepted answer. In fact, I only see about 5 answers, but I see it says there are 15Lychnis
What app are you using?Ardisardisj
@PaulD.Waite The SE one for android. on galaxy s3Lychnis
Aha. I don’t have the app to look myself, but here’s a link to the accepted answer: https://mcmap.net/q/73460/-how-do-you-check-that-a-number-is-nan-in-javascriptArdisardisj
@PaulD.Waite Ah, I see. I suppose, in direct answer to only the question in and of itself, that answer may suffice. I posted my answer on another question in total relation to "JS way to check if is 'really' NaN", since isNaN is NOT reliable. I was commented there and told this question would better suit my answer, thus moving it here. Simply just putting something helpful/useful out there in relation to this issue that will pop up on a Google search of the issue at hand, is it NaN?. On side note, my answer would be more acurate more often than selected answer.Lychnis
N
6

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'))
Nonego answered 14/8, 2015 at 15:32 Comment(2)
My environment only supports ECMAScript XP Home Edition :(Ardisardisj
@PaulD.Waite (I hope you meant IE 6 :D) No problem :-) Just remember that isNaN could get you in trouble and remember to use Number.isNaN in ECMAScript 2015 environments :-)Nonego
I
6

function isNotANumber(n) {
  if (typeof n !== 'number') {
    return true;
  } 
  return n !== n;
}
Iridium answered 30/8, 2017 at 5:26 Comment(1)
Appreciate the help!!Skutchan
B
5

I use underscore's isNaN function because in JavaScript:

isNaN(undefined) 
-> true

At the least, be aware of that gotcha.

Busoni answered 18/10, 2012 at 19:38 Comment(3)
I'm confused. Why would isNaN returning true when passed undefined be incorrect behavior? It IS true that undefined is not a number, is it not?Stemson
@Stemson NaN should not be considered equivalent to undefined, they are both special values with specific and different meanings. Consider this: I have a value but I'm not telling what it is, maybe it is a number and maybe it isn't, nevertheless, from your point of view, it's undefined.Pyrochemical
@Pyrochemical undefined may be something different than NaN, however it is still not a number so isNaN(undefined) should be (and is) trueHelve
B
5

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.

Bihari answered 20/3, 2014 at 14:13 Comment(0)
C
4

Maybe also this:

function isNaNCustom(value){
    return value.toString() === 'NaN' && 
           typeof value !== 'string' && 
           typeof value === 'number'
}
Cailean answered 1/2, 2018 at 13:30 Comment(0)
L
3

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);
Lesleylesli answered 6/5, 2013 at 9:5 Comment(1)
Sure, but I wasn't asking about Node.js.Ardisardisj
W
3
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..

Wey answered 8/11, 2014 at 22:18 Comment(0)
D
2

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.

Disendow answered 28/10, 2014 at 20:9 Comment(0)
P
2

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
Packer answered 30/11, 2015 at 6:47 Comment(1)
“Only simple way to find out if variable is NaN is an global function isNaN()” – not true; there is another simple way: var xIsNaN = x !== x; This yields true only if x is NaN.Gastelum
B
2

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

Bobo answered 4/5, 2016 at 17:22 Comment(0)
E
2

Found another way, just for fun.

function IsActuallyNaN(obj) {
  return [obj].includes(NaN);  
}
Esthete answered 19/5, 2017 at 4:43 Comment(0)
L
0

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'
Leaven answered 30/5, 2016 at 4:25 Comment(0)
H
0

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;
}
Heinous answered 5/6, 2016 at 20:14 Comment(0)
R
0

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.

function isNumber(i) {
    return !isNaN(i && i !== true ? Number(i) : parseFloat(i)) && [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY].indexOf(i) === -1;
}

console.log(isNumber(Infinity));
console.log(isNumber("asdf"));
console.log(isNumber(1.4));
console.log(isNumber(NaN));
console.log(isNumber(Number.MAX_VALUE));
console.log(isNumber("1.68"));

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

function isNumber(i) {//function for checking if parameter is number
if(!arguments.length) {
throw new SyntaxError("not enough arguments.");
	} else if(arguments.length > 1) {
throw new SyntaxError("too many arguments.");
	} else if([Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY].indexOf(i) !== -1) {
throw new RangeError("number cannot be \xB1infinity.");
	} else if(typeof i === "object" && !(i instanceof RegExp) && !(i instanceof Number) && !(i === null)) {
throw new TypeError("parameter cannot be object/array.");
	} else if(i instanceof RegExp) {
throw new TypeError("parameter cannot be RegExp.");
	} else if(i == null || i === undefined) {
throw new ReferenceError("parameter is null or undefined.");
	} else {
return !isNaN(i && i !== true ? Number(i) : parseFloat(i)) && (i === i);
	}
}
console.log(isNumber(Infinity));
console.log(isNumber(this));
console.log(isNumber(/./ig));
console.log(isNumber(null));
Rationality answered 9/6, 2017 at 1:11 Comment(2)
Your premise the infinity is technically not a number is technically flawed : math.stackexchange.com/questions/36289/is-infinity-a-number . From my reading of the answers is that infinity being a number depends on context.Strow
i'm going to move this to another questionRationality
D
0
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.

Detinue answered 16/8, 2017 at 19:38 Comment(0)
V
0
Number('hello').toString() === 'NaN' // true
Number(undefined).toString() === 'NaN' // true
    
Number('12345').toString() === 'NaN' // false  

// These all evaluate to 0 which is a number
Number('').toString() === 'NaN' // false // 0
Number('0').toString() === 'NaN' // false // 0
Number().toString() === 'NaN' // false // 0

// These all evaluate to 0 and 1 which is a number
Number(false).toString() === 'NaN' // false // 0
Number(true).toString() === 'NaN' // false // 1
Valentia answered 1/7, 2021 at 20:12 Comment(0)
H
0

Try both in condition

if(isNaN(parseFloat('geoff'))  && typeof(parseFloat('geoff')) === "number");
//true
Hawaiian answered 11/8, 2022 at 9:2 Comment(0)
L
0

Found this to be useful

    // Long-hand const isFalsey = (value) => {   if (
        value === null ||
        value === undefined ||
        value === 0 ||
        value === false ||
        value === NaN ||
        value === ""   ) {
        return true;   }   return false; };
 // Short-hand const 
      isFalsey = (value) => !value;
Leaflet answered 3/11, 2022 at 4:38 Comment(0)
G
-1

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

var val = Number("test");
if(String(val) === 'NaN') {
   console.log("true");
}
Gratuitous answered 23/2, 2017 at 11:17 Comment(0)
F
-1

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".

Fortis answered 18/9, 2017 at 21:57 Comment(1)
not very useful if you explicitly look for NaN values istead of looking for the oppositeCollogue
W
-2

So I see several responses to this,

But I just use:

function isNaN(x){
     return x == x && typeof x == 'number';
}
Wernick answered 18/12, 2017 at 22:57 Comment(1)
shouldn't it be return x != x && typeof x === 'number' ?Geodesic

© 2022 - 2024 — McMap. All rights reserved.