isNaN
Checks whether converting a value to a number gives NaN.
Available since: ES1
Syntax
isNaN(value)Parameters
| Parameter | Type | Description |
|---|---|---|
| value | any | A value; it is converted to a number first. |
Return value
booleantrue if converting it to a number gives NaN.
Examples
Checking strings:
console.log(isNaN('42'));
console.log(isNaN('hello'));Output:
false
trueSurprising cases caused by conversion:
console.log(isNaN(''));
console.log(isNaN(undefined));Output:
false
trueNotes
An empty string converts to 0, so isNaN('') is false. To check an actual number, Number.isNaN() is more reliable.
See also
- Number.isNaN — Checks that a value is NaN, without type conversion.
- NaN — The special Not-a-Number value — the result of a failed math operation.
- Number() — Converts a value to a number.
In the tutorial
- Numbers — Numbers in JavaScript — integers and fractions, rounding, the Math object, toFixed and the special values Infinity and NaN.