Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Number.isNaN()

BaselineWidely available

TheNumber.isNaN() static method determines whether the passed value is the number valueNaN, and returnsfalse if the input is not of the Number type. It is a more robust version of the original, globalisNaN() function.

Try it

function typeOfNaN(x) {  if (Number.isNaN(x)) {    return "Number NaN";  }  if (isNaN(x)) {    return "NaN";  }}console.log(typeOfNaN("100F"));// Expected output: "NaN"console.log(typeOfNaN(NaN));// Expected output: "Number NaN"

Syntax

js
Number.isNaN(value)

Parameters

value

The value to be tested forNaN.

Return value

The boolean valuetrue if the given value is a number with valueNaN. Otherwise,false.

Description

The functionNumber.isNaN() provides a convenient way to check for equality withNaN. Note that you cannot test for equality withNaN using either the== or=== operators, because unlike all other value comparisons in JavaScript, these evaluate tofalse whenever one operand isNaN, even if the other operand is alsoNaN.

Sincex !== x is only true forNaN among all possible JavaScript values,Number.isNaN(x) can also be replaced with a test forx !== x, despite the latter being less readable.

As opposed to the globalisNaN() function, theNumber.isNaN() method doesn't force-convert the parameter to a number. This makes it safe to pass values that would normally convert toNaN but aren't actually the same value asNaN. This also means that only values of the Number type that are alsoNaN returntrue.

Examples

Using isNaN()

js
Number.isNaN(NaN); // trueNumber.isNaN(Number.NaN); // trueNumber.isNaN(0 / 0); // trueNumber.isNaN(37); // false

Difference between Number.isNaN() and global isNaN()

Number.isNaN() doesn't attempt to convert the parameter to a number, so non-numbers always returnfalse. The following are allfalse:

js
Number.isNaN("NaN");Number.isNaN(undefined);Number.isNaN({});Number.isNaN("blabla");Number.isNaN(true);Number.isNaN(null);Number.isNaN("37");Number.isNaN("37.37");Number.isNaN("");Number.isNaN(" ");

The globalisNaN() coerces its parameter to a number:

js
isNaN("NaN"); // trueisNaN(undefined); // trueisNaN({}); // trueisNaN("blabla"); // trueisNaN(true); // false, this is coerced to 1isNaN(null); // false, this is coerced to 0isNaN("37"); // false, this is coerced to 37isNaN("37.37"); // false, this is coerced to 37.37isNaN(""); // false, this is coerced to 0isNaN(" "); // false, this is coerced to 0

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-number.isnan

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp