Number.isNaN()

O método Number.isNaN() determina se o valor passado é NaN (en-US). Esta é uma versão mais robusta que isNaN() (en-US).

Sintaxe

Number.isNaN(valor)

Parâmetros

valor
O valor a ser testado se é NaN (en-US).

Descrição

Due to both equality operators, == (en-US) and === (en-US), evaluating to false when checking if NaN (en-US) is NaN (en-US), the function Number.isNaN() has become necessary. This situation is unlike all other possible value comparisons in JavaScript.

In comparison to the global isNaN() (en-US) 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 (en-US), but aren't actually the same value as NaN (en-US). This also means that only values of the type number, that are also NaN (en-US), return true.

Exemplos

Number.isNaN(NaN);        // true
Number.isNaN(Number.NaN); // true
Number.isNaN(0 / 0)       // true

// e.g. these would have been true with global isNaN()
Number.isNaN("NaN");      // false
Number.isNaN(undefined);  // false
Number.isNaN({});         // false
Number.isNaN("blabla");   // false

// These all return false
Number.isNaN(true);
Number.isNaN(null);
Number.isNaN(37);
Number.isNaN("37");
Number.isNaN("37.37");
Number.isNaN("");
Number.isNaN(" ");

Polyfill

Number.isNaN = Number.isNaN || function(value) {
    return typeof value === "number" && isNaN(value);
}

Especificações

Especificação Estado Comentário
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Number.isnan' in that specification.
Standard Definição inicial.

Compatibilidade dos browsers

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help! (en-US)
Funcionalidade Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 25 15 (15) No support (Yes) 9
Funcionalidade Android Chrome para Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support No support ? 15.0 (15) No support No support iOS 9+

Ver também