Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number.isNaN doesn't exist in IE

Why does not IE support the Number.isNaN function? I can't use simple isNan instead of Number.isNaN 'cause these functions are different! For example:

Number.isNaN("abacada") != isNaN("abacada") //false != true

I need to abstract string and number and check, is some var really contains NaN (I mean NaN constant, but not-a-number value like strings).

someobj.someopt = "blah"/0;
someobj.someopt2 = "blah";
someobj.someopt3 = 150;
for(a in someobj) if(Number.isNaN(someobj[a])) alert('!');

That code should show alert for 1 time. But if I will change Number.isNaN to isNaN, it will alert 2 times. There are differences.

May be there is some alternative function exists?

like image 680
impulsgraw Avatar asked Nov 16 '14 21:11

impulsgraw


People also ask

What can I use instead of isNaN?

isNaN(x) can also be replaced with a test for x !== x , despite the latter being less readable.

Why is isNaN false?

That isNaN(" ") is false is part of the confusing behavior of the isNaN global function due to its coercion of non-numbers to a numeric type. From MDN: Since the very earliest versions of the isNaN function specification, its behavior for non-numeric arguments has been confusing.

How do I find my isNaN?

To check whether the given number is NaN or finite, we can use JavaScript methods. 1. isNaN() Method: To determine whether a number is NaN, we can use the isNaN() function. It is a boolean function that returns true if a number is NaN otherwise returns false.

Should I use isNaN or number isNaN?

isNaN() has become necessary. In comparison to the global isNaN() function, Number. isNaN() doesn't suffer the problem of forcefully converting the parameter to a number. This means it is now safe to pass values that would normally convert to NaN, but aren't actually the same value as NaN.


1 Answers

Why does not IE support the Number.isNaN function?

That's rather off-topic for Stack Overflow, but according to the MDN page for Number.isNaN, it's not defined in any standard — it's only in the draft spec for ECMAScript 6 — and Internet Explorer is not the only browser that doesn't support it.

May be there is some alternative function exists?

Not really, but you can easily define one:

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

or if you prefer:

function myIsNaN(o) {
    return o !== o;
}
like image 154
ruakh Avatar answered Sep 28 '22 19:09

ruakh