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.

Number static methodES2015Live demo
Common call
Number.isNaN(x)
Returns
boolean
Replaces
the global isNaN, and the x !== x trick
Watch out
the global coerces; this one does not
Number.isNaN(valuevalue — Any value. Only the actual NaN number returns true — strings, undefined and objects are all false regardless of what they would convert to.type: any · required)
→ boolean

Demo

Live evaluation
Try:
Inputs
vanyany value
Output
[Number.isNaN('abc'), isNaN('abc')]
[false, true]

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

NameTypeRequiredDescription
valueanyyesAny 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

Check a computation result
The legitimate use — did the arithmetic fail?
const n = Number(input);
if (Number.isNaN(n)) showError();
Validate that a string is numeric
What the global isNaN gets misused for.
const isNumeric = s => s.trim() !== "" && !Number.isNaN(Number(s));
Object.is works too
NaN equals itself under SameValue.
Object.is(x, NaN);

Examples

1. Actual NaN
Number.isNaN(NaN)
Returns
true
2. A string is not
Number.isNaN('abc')
Returns
false
3. The global says yes
isNaN('abc')
Returns
true
4. undefined
isNaN(undefined)
Returns
true
5. But it is not NaN
Number.isNaN(undefined)
Returns
false
6. NaN is not itself
NaN === NaN
Returns
false

Pitfalls

1. The global isNaN coerces first
isNaN("abc") is true, isNaN(undefined) is true, isNaN({}) is true — none of those values IS NaN. The global is a numeric-validity test wearing the wrong name, and using it to check a computation result gives false positives for every non-number that passes through.
Coerces
isNaN('abc')
true
Does not
Number.isNaN('abc')
false
2. NaN is not equal to itself
Every comparison involving NaN is false, including NaN === NaN. That is why a dedicated predicate is needed at all, and why a value that has become NaN silently fails every equality check downstream rather than throwing.
Never true
const x = 0/0;
x === NaN
false
Use the predicate
Number.isNaN(0/0)
true
3. Using it to validate input
It only answers whether a value already IS NaN. To check whether a string represents a number you must convert first — and handle the empty string, which converts to 0 rather than NaN.
Always false for strings
Number.isNaN('')
false
Convert, then check
const s = '';
s.trim() !== '' && !Number.isNaN(Number(s))
false
4. NaN propagates silently through arithmetic
Once one operand is NaN every subsequent result is NaN, with no error anywhere. A single bad parse at the start of a pipeline surfaces as NaN in the output, far from its cause.
Spreads
const t = Number('x') + 1 + 2
NaN
Check at the boundary
const n = Number('x');
if (Number.isNaN(n)) throw new Error('bad input');
fails early

When to use

Use it
  • Testing whether a computation produced NaN
  • Validating the result of Number() or parseFloat
  • Any check where a non-number must NOT count as NaN
Reach for something else
  • 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

Complexity
O(1)
Return
A boolean; nothing is allocated or coerced
CPython impl
V8: Builtins-number-isnan
Memory
No allocation
Thread-safe
Single-threaded

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?'

History

ES1
The global isNaN present from the start, with its coercing behaviour.
ES2015
Number.isNaN added as the non-coercing version, alongside Number.isFinite and Number.isInteger.