Number.isNaN()
isNaN(number) The Number.isNaN() method determines whether the passed value is NaN. Unlike the global isNaN() function, this method does not coerce its argument to a number, making it more reliable for type checking.
Syntax
Number.isNaN(value)
Parameters
| Parameter | Type | Description |
|---|---|---|
value | any | The value to check |
Return Value
Returns true if the value is exactly NaN, false otherwise.
Once you understand the function signature and return behavior, the examples below walk through progressively more realistic scenarios—from the basic check through common NaN-producing operations to practical patterns like safe division and calculator error handling.
Examples
Basic Usage
Number.isNaN(NaN); // true
Operations that produce NaN
The simplest case is checking the literal NaN value, but NaN typically appears as the result of an operation. Division of zero by zero, multiplication involving NaN, taking the square root of a negative number, or coercing a non-numeric string all produce NaN. Each case listed below shows a distinct way that the NaN value can appear in real code.
Number.isNaN(0/0); // true (division of zero by zero)
Number.isNaN(0 * NaN); // true (multiplication with NaN)
Number.isNaN(Math.sqrt(-1)); // true (square root of negative)
Number.isNaN(Number('hello')); // true (invalid number parsing)
Values that are not NaN
The operations above all return NaN, but other values—numbers, strings, undefined, null, and objects—are not NaN even when they might seem conceptually “empty” or “missing.” This is where the strictness of Number.isNaN() becomes important, because the global isNaN() would misclassify many of these.
Number.isNaN(42); // false
Number.isNaN('42'); // false (string, not NaN)
Number.isNaN(undefined); // false
Number.isNaN(null); // false
Number.isNaN({}); // false
Comparison: Number.isNaN() vs global isNaN()
The global isNaN() coerces its argument to a number first, which produces surprising results for non-numeric inputs. A string like 'hello' becomes NaN after coercion, so isNaN('hello') returns true even though the input is clearly a string. Number.isNaN() skips the coercion step entirely—it returns true only when the value is already the NaN number, making it the safer choice for type-aware code.
isNaN('hello'); // true (coerced to NaN)
Number.isNaN('hello'); // false (not the number NaN)
isNaN('42'); // true (coerced to NaN... wait, no: '42' becomes 42!)
Number.isNaN('42'); // false
isNaN(undefined); // true (coerced to NaN)
Number.isNaN(undefined); // false
Practical example: safe division
The comparison above clarifies why Number.isNaN() is the better check. The next example applies it to a real-world function—safe division—where a zero denominator should produce NaN rather than Infinity or throw an error, and the check confirms the result is usable before returning it.
function safeDivide(a, b) {
if (b === 0) {
return NaN;
}
const result = a / b;
return Number.isNaN(result) ? NaN : result;
}
safeDivide(10, 2); // 5
safeDivide(10, 0); // NaN
safeDivide(0, 0); // NaN
Practical example: calculator results
When building a calculator, you need to handle NaN results gracefully. The safe division example focused on one operation; a calculator must check results across multiple operators, any of which can produce NaN when given invalid inputs. Wrapping the check in a single guard at the end keeps the logic centralized.
function calculate(a, b, operator) {
let result;
switch (operator) {
case '+': result = a + b; break;
case '-': result = a - b; break;
case '*': result = a * b; break;
case '/': result = b !== 0 ? a / b : NaN; break;
default: result = NaN;
}
if (Number.isNaN(result)) {
return 'Invalid operation';
}
return result;
}
calculate(10, 5, '+'); // 15
calculate(10, 0, '/'); // "Invalid operation"
calculate(10, 5, '^'); // "Invalid operation"
How Number.isNaN() works
Number.isNaN() checks whether the argument is exactly the NaN value. It does not coerce its argument — if you pass a string, an object, or any non-number type, it returns false immediately, because only the actual NaN number value can return true. This is the key difference from the global isNaN() function, which first converts the argument to a number and then tests the result.
What NaN is
NaN (Not-a-Number) is a special floating-point value that results from arithmetic operations that cannot produce a meaningful numeric result — dividing zero by zero, taking the square root of a negative number, parsing a non-numeric string with Number(), or any arithmetic involving NaN. Despite its name, typeof NaN === 'number', which is why a dedicated check is necessary.
why NaN !== NaN
NaN is the only JavaScript value that is not equal to itself. This is specified in the IEEE 754 standard for floating-point arithmetic. It means you cannot detect NaN with ===. Number.isNaN(value) is the correct way; alternatively, Object.is(value, NaN) works for the same reason as Number.isNaN.
const x = NaN;
x === NaN; // false — NaN is never equal to itself
Number.isNaN(x); // true
Object.is(x, NaN); // true
When Number.isNaN() is the right check
Number.isNaN() is the right check whenever the value may come from outside your code and you want to know whether it is the actual NaN value. That includes math helpers, parser output, and any calculation that can fail without throwing. Because it does not coerce, it gives you a clean answer instead of trying to interpret strings or other input types on your behalf.
That strictness is what makes it safer than the global isNaN() function in modern code. If you only want to accept the one special floating-point value, Number.isNaN() makes that rule obvious and avoids accidental truthy results from data that merely becomes NaN after conversion.
See Also
Number.isFinite()— Checks if a value is a finite numberNumber.isInteger()— Checks if a value is an integerMath.sqrt()— Computes the square root of a number (returns NaN for negative inputs)Object.is()— Compares two values for equality (handles NaN correctly)