目录

NaN

描述 (Description)

不带引号的文字常量NaN是表示非数字的特殊值。 由于NaN始终将不等于任何数字(包括NaN,进行比较NaN,因此通常用于指示应返回有效数字的函数的错误条件。

Note - 使用isNaN()全局函数查看值是否为NaN值。

语法 (Syntax)

使用NaN的语法是 -

var val = Number.NaN;

例子 (Example)

请尝试以下示例以了解如何使用NaN。

<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.")
               }
               Document.write("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>

输出 (Output)

新页面打开
↑回到顶部↑
WIKI教程 @2018