The JavaScript Number.isNaN() method
The Number.isNaN() method returns true only when a value is the special NaN value. Number.isNaN(NaN) is true; Number.isNaN("abc") is false. Use it instead of the older global isNaN(), which coerces its argument first and gives misleading results.
Overview
Number.isNaN() exists to solve a genuinely awkward problem: NaN is the only value in JavaScript that isn't equal to itself, so NaN === NaN is false and you can't test for it with a normal comparison. Number.isNaN(value) is the reliable check, returning true only when the value really is NaN.
It replaces the older global isNaN(), which has a notorious flaw: it coerces its argument to a number first. So isNaN("abc") is true (because "abc" converts to NaN) even though "abc" isn't the NaN value — that coercion produces confusing results. Number.isNaN() does no coercion: it returns true only for an actual NaN, so Number.isNaN("abc") is correctly false.
You hit NaN whenever math goes wrong — parseInt("x"), 0/0, Number(undefined). After parsing user input or doing arithmetic that might fail, Number.isNaN() is how you detect the failure and respond. It sits alongside Number.isInteger() and Number.isFinite() as the modern, coercion-free numeric checks.
Syntax
Number.isNaN(value)
Number.isNaN(NaN) // true
Number.isNaN(0 / 0) // true
Number.isNaN("abc") // false (no coercion)
isNaN("abc") // true (global, coerces - avoid)
Parameters
The Number.isNaN() method accepts the following parameters.
| Parameter | Description |
|---|---|
value |
The value to test. Returns true only if it is exactly the NaN value. |
Example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
const result = parseInt('not a number', 10);
document.getElementById('out').textContent =
'value: ' + result + '\n' +
'is NaN? ' + Number.isNaN(result); // value: NaN / is NaN? true
</script>
Best practices
- Use
Number.isNaN(), not the globalisNaN(), to avoid the coercion bug. - Check for
NaNafter parsing input or doing math that could fail. - Never test with
value === NaN— it's alwaysfalsebecauseNaNisn't equal to itself. - Pair it with Number.isInteger() and
Number.isFinite()for thorough validation.
Frequently asked questions
How do I check for NaN in JavaScript?
Number.isNaN(value). Don't use value === NaN, which is always false.What is the difference between Number.isNaN() and the global isNaN()?
isNaN() coerces its argument to a number first, so isNaN("abc") is true. Number.isNaN() does no coercion and returns true only for the actual NaN value.Why is NaN === NaN false?
NaN is defined as not equal to anything, including itself. That's exactly why Number.isNaN() exists.When does a value become NaN?
parseInt("x"), 0/0, Number(undefined) and similar.