impulsgraw
impulsgraw

Reputation: 897

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?

Upvotes: 18

Views: 16587

Answers (2)

Łukasz Wojtanowski
Łukasz Wojtanowski

Reputation: 149

You need use isNaN without "Number." Like this:

for(a in someobj) if(isNaN(someobj[a])) alert('!');

This also work fine for chrome.

You can find more on microsoft site https://learn.microsoft.com/en-us/scripting/javascript/reference/isnan-function-javascript

Upvotes: 1

ruakh
ruakh

Reputation: 183311

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;
}

Upvotes: 22

Related Questions