JavaScriptNumbers and Math
Number. isNaN()
Checks whether a value is exactly NaN.
- Returns
trueorfalse.
Example
#JavaScript
const age = Number('twenty')
console.log(age)
console.log(Number.isNaN(age))
console.log(age === NaN)
console.log(Number.isNaN('abc'))
console.log(isNaN('abc'))Console
NaN
true
false
false
true
Definition and usage
#The Number.isNaN() function returns true only when the given value is the number NaN (Not a Number). NaN shows up, for example, after a failed conversion from text to a number, or after dividing 0 / 0.
You can’t check this with a comparison, because NaN is the only value in JavaScript that isn’t equal to itself: NaN === NaN returns false.
The older, global isNaN() function first converts its argument to a number, so it also returns true for ordinary strings like "abc". Number.isNaN() doesn’t do that and is more predictable.
Syntax
#Syntax
Number.isNaN(value)Parameters
#value
any value
The value being checked.
Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- After every conversion from text to a number, check the result with
Number.isNaN(). - Avoid the global
isNaN(), since it silently converts types.
Related entries
#- Number.isInteger()Checks whether a value is an integer.
- Number.parseInt()Converts the start of a string into an integer in a given numeral system.
- ===The strict equality operator compares values without type conversion: true only for the same type and the same value.
- Number.isFinite()Checks whether a value is an ordinary, finite number, not NaN or infinity.
Spotted a mistake or missing an example? Write to us.