NaN, short for Not a Number, is used to indicate when an operation intended to return a number has failed.
In ECMAScript, dividing a number by 0 returns NaN, which allows other processing to continue.
Any operation involving NaN always returns NaN.
NaN is not equal to any value, including NaN. For example, the following returns false:
console.log(NaN == NaN); //false
ECMAScript isNaN() function accepts a single argument, which can be of any data type, to determine if the value is "not a number."
When a value is passed into isNaN(), the value is converted into a number.
Some non-number values can be converted numbers, such as the string "10" or a Boolean value.
Any value that cannot be converted into a number causes the function to return true. Consider the following:
console.log(isNaN(NaN)); //true console.log(isNaN(10)); //false - 10 is a number console.log(isNaN("10")); //false - can be converted to number 10 console.log(isNaN("blue")); //true - cannot be converted to a number console.log(isNaN(true)); //false - can be converted to number 1
This example tests five different values.
isNaN() can be used on objects. The object's valueOf() method is first called to determine if the returned value can be converted into a number.
If not, the toString() method is called and its returned value is tested as well.