Home > Web Front-end > JS Tutorial > body text

How to use NaN values ​​in JavaScript_Basics

WBOY
Release: 2016-05-16 15:56:32
Original
1198 people have browsed it

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;

Copy after login

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>

Copy after login

This will produce the following results:

Day of Month must be between 1 and 31. Value of dayOfMonth : NaN
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!