16

Number.isNaN() - JavaScript | MDN

 4 years ago
source link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Number.isNaN() - JavaScript | MDN

The Number.isNaN() method determines whether the passed value is NaN and its type is Number. It is a more robust version of the original, global isNaN().

Syntax

Number.isNaN(value)

Parameters

value The value to be tested for NaN.

Return value

true if the given value is NaN and its type is Number; otherwise, false.

Description

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

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. This also means that only values of the type number, that are also NaN, return true.

Polyfill

The following works because NaN is the only value in javascript which is not equal to itself.

Number.isNaN = Number.isNaN || function isNaN(input) {
    return typeof input === 'number' && input !== input;
}

Examples

Using isNaN

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(' ');

Specifications

Specification
ECMAScript (ECMA-262)
The definition of 'Number.isnan' in that specification.

Browser compatibility

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
isNaN Full support 25 Full support 12 Full support 15 No support No Full support 15 Full support 9 Full support ≤37 Full support 25 Full support 15 Full support 14 Full support 9 Full support 1.5 Full support 0.10

Legend

Full support   Full support No support   No support

See also


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK