Home > Article > Web Front-end > How to use NaN values in JavaScript_Basics
The literal constant NaN without quotes is a special value, which means it is not a non-number. Since NaN always compares to any number, including NaN, it is usually an error condition used to indicate a function that should return a valid number.
Note: Use the isNaN() global function to see if a value is a NaN value.
Grammar
You can access properties using the following syntax:
var val = Number.NaN;
Example:
Here, dayOfMonth assigns NaN if it is greater than 31 and displays a message indicating the valid range:
<html> <head> <script type="text/javascript"> <!-- function showValue() { var dayOfMonth = 50; if (dayOfMonth < 1 || dayOfMonth > 31) { dayOfMonth = Number.NaN alert("Day of Month must be between 1 and 31.") } alert("Value of dayOfMonth : " + dayOfMonth ); } //--> </script> </head> <body> <p>Click the following to see the result:</p> <form> <input type="button" value="Click Me" onclick="showValue();" /> </form> </body> </html>
This will produce the following results:
Day of Month must be between 1 and 31. Value of dayOfMonth : NaN