JavaScript isNaN() vs. Number.isNaN(): How to check for NaN
Global isNaN(value) converts the argument and checks whether the result is NaN. Number.isNaN(value) checks for the actual Number value NaN without conversion. Neither alone checks whether an input is a valid finite number.
Tell NaN from text that is not numeric
isNaN(NaN); // true
isNaN("text"); // true: numeric conversion produces NaN
Number.isNaN(NaN); // true
Number.isNaN("text"); // false: the string is not the NaN value
NaN means Not-a-Number. It can result from 0 / 0 or converting nonnumeric text. The string "NaN" is another value. typeof NaN gives "number". isNaN() converts before testing, while Number.isNaN() returns true only for actual NaN.
| Value | isNaN() |
Number.isNaN() |
|---|---|---|
NaN |
true |
true |
"text" |
true |
false |
undefined |
true |
false |
"12" |
false |
false |
"" |
false |
false |
null |
false |
false |
Infinity |
false |
false |
An empty string and null become 0 in the global function, so its false result does not show that the user entered a number. The string "123 * 123" cannot be evaluated by numeric conversion. Use recognized input formats without evaluating user text as code.
Compare a finite result, NaN, and Infinity
Save the example as nan-check.html and open it. The buttons divide fixed values and the output is written with textContent.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Distinguish NaN and Infinity</title>
</head>
<body>
<h1>Check division results</h1>
<button type="button" data-a="6" data-b="2">6 ÷ 2</button>
<button type="button" data-a="0" data-b="0">0 ÷ 0</button>
<button type="button" data-a="1" data-b="0">1 ÷ 0</button>
<pre id="output"></pre>
<script>
const output = document.getElementById("output");
function show(a, b) {
const value = a / b;
output.textContent = [
`${a} / ${b} = ${value}`,
`typeof: ${typeof value}`,
`isNaN: ${isNaN(value)}`,
`Number.isNaN: ${Number.isNaN(value)}`,
`Number.isFinite: ${Number.isFinite(value)}`
].join("\n");
}
document.querySelectorAll("button[data-a]").forEach((button) => {
button.addEventListener("click", () => {
show(Number(button.dataset.a), Number(button.dataset.b));
});
});
show(6, 2);
</script>
</body>
</html>
6 ÷ 2 returns 3, both NaN checks are false and the finite check is true. 0 ÷ 0 produces NaN and both NaN checks are true. 1 ÷ 0 produces Infinity. Its NaN and finite checks are both false. Press 6 ÷ 2 to return to a finite result. Number division by zero does not throw here.
Choose a check for equality or input validation
NaN === NaN; // false
NaN == NaN; // false
Number.isNaN(NaN); // true
NaN === NaN and NaN == NaN are both false. Use Number.isNaN(value) to detect NaN. Symbol or BigInt can make the global function throw TypeError. For a form, check blanks and format, convert explicitly, and test the result with Number.isFinite(). Add integer and range rules as needed.
Identify why a calculation result is invalid
Run this code in the Console. It first checks for a Number, then uses Number.isNaN() for NaN. A Number that is neither NaN nor finite is positive or negative infinity.
{
function classifyResult(value) {
if (typeof value !== "number") return "not-number";
if (Number.isNaN(value)) return "nan";
if (!Number.isFinite(value)) return "infinite";
return "finite";
}
for (const value of [0 / 0, 1 / 0, "3", 6 / 2, 0]) {
console.log(classifyResult(value));
}
}
The output is nan, infinite, not-number, finite, finite. Zero is finite. Use the classification for distinct messages. To split the infinity cases, test value === Infinity inside that branch.




