NaN (Not a Number) represents an invalid value in JavaScript, which is a special value that cannot be interpreted as a number. NaN usually results from doing math on non-numeric strings, doing math on null or undefined values, trying to convert a non-number to a number, or performing invalid operations on numbers. Use the isNaN() function to check whether a value is NaN, which has the special properties of not being equal to any other value (including itself), not being comparable via comparison operators, and being the only non-finite value. When handling NaN, you can check the input, use the isNaN() function to identify and handle, use default values or fallback logic, etc.
What is NaN?
NaN (Not a Number) represents an invalid value in JavaScript. It is a special numeric value that indicates a value that cannot be interpreted as a number.
How is NaN generated?
The following are some common situations that cause NaN to occur:
"hello" 1
undefined * 5
parseInt(" foo")
Math.sqrt(-1)
Recognize NaN
You can use the isNaN()
function to check whether a value is NaN. This function returns a Boolean value indicating whether the value is NaN:
<code class="javascript">console.log(isNaN(NaN)); // true console.log(isNaN(123)); // false</code>
Special properties of NaN
NaN has the following special properties:
NaN !== NaN
Handling NaN
When you encounter NaN, here are some steps you can take:
isNaN()
Function identifies and handles NaN valuesThe above is the detailed content of What is nan in js. For more information, please follow other related articles on the PHP Chinese website!