Number.isNaN()
Added because the global isNaN was misnamed. The global asks "would this become NaN if converted to a number", which is true for every non-numeric string.
Demo
The pair is [Number.isNaN(v), isNaN(v)]. The first case is the whole reason this method exists: for the string 'abc' the two disagree — Number.isNaN says false, because a string is not the NaN number, while the global says TRUE, because converting 'abc' to a number would produce NaN. The global is really asking 'is this un-numeric', which is a different question and almost never the one you meant. For genuine numbers the two always agree.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| value | any | yes | Any value. Only the actual NaN number returns true — strings, undefined and objects are all false regardless of what they would convert to. |
Return value
boolean — True only if the value IS the NaN number. No coercion — a non-number argument is simply false, never converted first.
Common patterns
const n = Number(input); if (Number.isNaN(n)) showError();
const isNumeric = s => s.trim() !== "" && !Number.isNaN(Number(s));
Object.is(x, NaN);
Examples
Pitfalls
isNaN('abc')
Number.isNaN('abc')
const x = 0/0; x === NaN
Number.isNaN(0/0)
Number.isNaN('')
const s = ''; s.trim() !== '' && !Number.isNaN(Number(s))
const t = Number('x') + 1 + 2
const n = Number('x'); if (Number.isNaN(n)) throw new Error('bad input');
When to use
- Testing whether a computation produced NaN
- Validating the result of Number() or parseFloat
- Any check where a non-number must NOT count as NaN
- Checking whether a string is numeric → convert first, then test
- You want one predicate over all values → Object.is(x, NaN)
- Checking for finiteness → Number.isFinite
- The global isNaN → almost never the right question
Notes
FAQ
The global converts its argument to a number first, so it returns true for anything un-numeric — strings, undefined, objects. Number.isNaN performs no conversion and returns true only for the NaN value itself. The global is almost always the wrong question.
isNaN('abc'); // true — 'would this be NaN?' Number.isNaN('abc'); // false — 'is this NaN?'