The isNaN()
function is a JavaScript function that stands for "is Not a Number." It is used to determine whether a value is NaN (Not a Number) or not. In JavaScript, the NaN
value is a special value that represents the result of an invalid or undefined mathematical operation.
The syntax of the isNaN()
function is as follows:
isNaN(value)
Here, value
is the value that you want to check for being NaN. The function returns true
if the value is NaN, and false
otherwise.
It's important to note that isNaN()
can be a bit tricky because it attempts to convert the parameter to a number before checking whether it's NaN. If the conversion to a number is not possible, it returns true
. For example:
isNaN("Hello"); // true, because "Hello" cannot be converted to a number
isNaN("123"); // false, because "123" can be converted to the number 123
To check specifically if a value is NaN without any implicit type conversion, you can use the Number.isNaN()
method introduced in ECMAScript 2015 (ES6):
Number.isNaN(value)
This method only returns true
if the provided value is exactly NaN and returns false
for any other value, even if it would be coerced into NaN through the standard isNaN()
method.