What is NaN?
- NaN is a special value that means Not a Number.
- It means that a value that should be a number could not be parsed as a number.
- Any mathematical operation between any other value and NaN will result in NaN.
- It usually means something has gone wrong with a number parsing or mathematical operation and that you need to check your code or handle input differently.
- VALUE is a value, or variable, to check
- isNaN() will return a boolean value (TRUE or FALSE)
var myNumber = 4.32; var myString = "foo"; var myNumberString = "1234"; var myNaN = NaN; console.log(isNaN(undefined)); // true - undefined evaluates as NaN console.log(isNaN(myNumber)); // false - 4.32 evaluates as a number console.log(isNaN(myString)); // true - the given string "foo" cannot be parsed as a number console.log(isNaN(myNumberString)); // false - "1234" is a string which can be parsed as a number console.log(isNaN(myNumber * myString)); // true - the result of multiplying 4.32 and "foo" could not be calculated as a number console.log(isNaN(myNaN)); // true - the value is already NaN console.log(isNaN(2 * myNumber)); // false - the multiplication result of two numbers is a number console.log(isNaN(myNumber * myNaN)); // true - as the result of any maths involving NaN is NaN
console.log() is used to output the return value of each example of isNaN().