The isNaN()
function checks if a value is NaN (Not-a-Number) or not.
Example
let number = NaN;
// check if number is NaN
let result = isNaN(number);
console.log("Is number a NaN?", result);
// Output: Is number a NaN? true
isNaN() Syntax
The syntax of the isNaN()
function is:
isNaN(value)
isNaN() Parameters
The isNaN()
function takes in:
- value - The value to be tested.
isNaN() Return Value
- Returns
true
if the argument isNaN
. - Returns
false
for other arguments.
Example: Using isNaN()
console.log(isNaN(NaN)); // true
console.log(isNaN(undefined)); // true
console.log(isNaN(643511)); // false
console.log(isNaN(null)); // false
// inplicit conversion to number
console.log(isNaN("3888.415")); // false
console.log(isNaN("210AA")); // true as Number("210AA") is NaN
console.log(isNaN("")); // false as Number('') is 0
console.log(isNaN(new Date())); // false
console.log(isNaN(new Date().toString())); // true
Output
true true false false false true false false true
Notes:
isNaN()
is a top-level function and is not associated with any object.- If the argument is not of type Number, the value is first coerced to
NaN
and then checked.
Also Read: