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

在JavaScript中使用NaN值的方法_基础知识

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

 不带引号的字面常量NaN是一个特殊的值,表示不是非数字。由于NaN总是比较不等的情况,以任何数,包括NaN,它通常是用于指示应该返回一个有效的数的函数的错误条件。

注意:使用isNaN()全局函数来查看是否值是NaN值。
语法

您可以使用以下语法访问属性:

var val = Number.NaN;

Copy after login

示例 :

在这里,dayOfMonth分配NaN,如果是大于31,并显示一条消息,表明有效范围:

<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

这将产生以下结果:

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!