MDN may have intermittent access issues April 18 13:00 - April 19 01:00 UTC. See whistlepig.mozilla.org for all notifications.

mozilla

Revision 645913 of NaN

  • Revision slug: Web/JavaScript/Reference/Global_Objects/NaN
  • Revision title: NaN
  • Revision id: 645913
  • Created:
  • Creator: ziyunfei
  • Is current revision? No
  • Comment

Revision Content

{{JSObjectsQLAlpha()}}

概述

全局属性 NaN 表示 Not-A-Number 的值。

{{js_property_attributes(0,0,0)}}

语法

NaN

描述

NaN 是一个全局对象的属性。

NaN 属性的初始值就是 NaN,和 Number.NaN 的值一样。在现代浏览器中(ES5中), NaN 属性是一个不可配置(non-configurable),不可写(non-writable)的属性。但在ES3中,这个属性的值是可以被更改的,但是也应该避免覆盖。

在编码很少直接使用到 NaN。通常都是在计算失败时,作为 Math 的某个方法的返回值出现的(例如:Math.sqrt(-1))或者尝试将一个字符串解析成数字但失败了的时候(例如:parseInt("blabla"))。

判断一个值是否是NaN

等号运算符(=====) 不能被用来判断一个值是否是 NaN。必须使用 {{jsxref("Number.isNaN()")}} 或 {{jsxref("Global_Objects/isNaN", "isNaN()")}} 函数。

NaN === NaN;        // false
Number.NaN === NaN; // false
isNaN(NaN);         // true
isNaN(Number.NaN);  // true

规范

规范版本 规范状态 注解
ECMAScript 1st Edition. Standard Initial definition. Implemented in JavaScript 1.3
{{SpecName('ES5.1', '#sec-15.1.1.1', 'NaN')}} {{Spec2('ES5.1')}}  
{{SpecName('ES6', '#sec-value-properties-of-the-global-object-nan', 'NaN')}} {{Spec2('ES6')}}  

浏览器兼容性

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}

相关链接

  • {{jsxref("Number.NaN")}}
  • {{jsxref("Number.isNaN()")}}
  • {{jsxref("Global_Objects/isNaN", "isNaN()")}}

Revision Source

<div>
 {{JSObjectsQLAlpha()}}</div>
<h2 id=".E6.A6.82.E8.BF.B0">概述</h2>
<p>全局属性 <strong><code>NaN</code></strong> 表示 Not-A-Number 的值。</p>
<p>{{js_property_attributes(0,0,0)}}</p>
<h2 id=".E8.AF.AD.E6.B3.95">语法</h2>
<pre class="syntaxbox  language-html" data-prism-prevent-line-number="1" style="margin-bottom: 0px; padding: 1em; border-left-width: 6px; border-left-style: solid; border-left-color: rgba(0, 83, 159, 0.65098); font-family: Consolas, Monaco, 'Andale Mono', monospace; font-size: 14px; direction: ltr; white-space: normal; word-break: normal; tab-size: 4; text-shadow: none; background-color: rgba(212, 221, 228, 0.498039);">
<code class="language-html" style="font-family: Consolas, Monaco, 'Andale Mono', monospace; direction: ltr; word-spacing: normal; word-break: normal; tab-size: 4; color: inherit; text-shadow: none;">NaN</code></pre>
<h2 id="Description" name="Description">描述</h2>
<p><code>NaN</code>&nbsp;是一个全局对象的属性。</p>
<p><font face="monospace">NaN&nbsp;</font>属性的初始值就是 NaN,和&nbsp;<code>Number.NaN</code>&nbsp;的值一样。在现代浏览器中(ES5中),&nbsp;<code>NaN</code>&nbsp;属性是一个不可配置(non-configurable),不可写(non-writable)的属性。但在ES3中,这个属性的值是可以被更改的,但是也应该避免覆盖。</p>
<p>在编码很少直接使用<code>到 NaN</code>。通常都是在计算失败时,作为 Math 的某个方法的返回值出现的(例如:<code>Math.sqrt(-1)</code>)或者尝试将一个字符串解析成数字但失败了的时候(例如:<code>parseInt("blabla")</code>)。</p>
<h3 id=".E5.88.A4.E6.96.AD.E4.B8.80.E4.B8.AA.E5.80.BC.E6.98.AF.E5.90.A6.E6.98.AFNaN"><code>判断一个值是否是NaN</code></h3>
<p>等号运算符(<code>==</code> 和 <code>===)</code>&nbsp;不能被用来判断一个值是否是&nbsp;<code>NaN</code>。必须使用 {{jsxref("Number.isNaN()")}} 或 {{jsxref("Global_Objects/isNaN", "isNaN()")}}&nbsp;函数。</p>
<pre class="brush: js">
NaN === NaN;        // false
Number.NaN === NaN; // false
isNaN(NaN);         // true
isNaN(Number.NaN);  // true
</pre>
<h2 id=".E8.A7.84.E8.8C.83" style="margin-bottom: 20px; line-height: 30px;">规范</h2>
<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范版本</th>
   <th scope="col">规范状态</th>
   <th scope="col">注解</th>
  </tr>
  <tr>
   <td>ECMAScript 1st Edition.</td>
   <td>Standard</td>
   <td>Initial definition. Implemented in JavaScript 1.3</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.1.1.1', 'NaN')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-value-properties-of-the-global-object-nan', 'NaN')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>
<h2 id=".E6.B5.8F.E8.A7.88.E5.99.A8.E5.85.BC.E5.AE.B9.E6.80.A7" style="margin-bottom: 20px; line-height: 30px;">浏览器兼容性</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
 <table class="compat-table">
  <tbody>
   <tr>
    <th style="line-height: 16px;">Feature</th>
    <th style="line-height: 16px;">Chrome</th>
    <th style="line-height: 16px;">Firefox (Gecko)</th>
    <th style="line-height: 16px;">Internet Explorer</th>
    <th style="line-height: 16px;">Opera</th>
    <th style="line-height: 16px;">Safari</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<div id="compat-mobile">
 <table class="compat-table">
  <tbody>
   <tr>
    <th style="line-height: 16px;">Feature</th>
    <th style="line-height: 16px;">Android</th>
    <th style="line-height: 16px;">Chrome for Android</th>
    <th style="line-height: 16px;">Firefox Mobile (Gecko)</th>
    <th style="line-height: 16px;">IE Mobile</th>
    <th style="line-height: 16px;">Opera Mobile</th>
    <th style="line-height: 16px;">Safari Mobile</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<h2 id="See_also" name="See_also" style="margin-bottom: 20px; line-height: 30px;">相关链接</h2>
<ul>
 <li>{{jsxref("Number.NaN")}}</li>
 <li>{{jsxref("Number.isNaN()")}}</li>
 <li>{{jsxref("Global_Objects/isNaN", "isNaN()")}}</li>
</ul>
Revert to this revision