Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1017890 of Number.isNaN()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Number/isNaN
  • Revision title: Number.isNaN()
  • Revision id: 1017890
  • Created:
  • Creator: PierreNeter
  • Is current revision?
  • Comment

Revision Content

{{JSRef}}

Phương thức Number.isNaN() (NaN viết tắt cho Not a Number) xác định xem giá trị đó có chính xác là {{jsxref("NaN")}} hay không. Một phiên bản khác của phương thức này, nhưng nằm trong phạm vi global đó là: {{jsxref("isNaN", "isNaN()")}}.

Syntax

Number.isNaN(value)

Parameters

value
Giá trị truyền vô để kiểm tra xem nó có phải chính xác là {{jsxref("NaN")}} hay không.

Description

Chả hai phương pháp operator so sánh bằng, đó là {{jsxref("Operators/Comparison_Operators", "==", "#Equality")}} và {{jsxref("Operators/Comparison_Operators", "===", "#Identity")}} đề trả ra false khi chúng ta kiểm tra giá trị {{jsxref("NaN")}} {{jsxref("NaN")}} ví dụ: NaN === NaN, (đáng lẽ ra là true đúng không ?, nhưng thực tế giá trị NaN này không thể được xác định bằng cách này.), phương thức Number.isNaN() được tạo ra để làm việc này. Phương pháp này đưa ra 1 giải pháp để xác định NaN, NaN là một giá trị mà không thể so sánh với chính nó để kiểm tra như các giá trị khác trong JavaScript.

{{jsxref("isNaN", "isNaN()")}} là phương thức kiểm tra NaN trong phạm vi global, lưu ý là Number.isNaN() không giống như isNaN() trong global, Number.isNaN() xác định chính xác {{jsxref("NaN")}} đó có phải là giá trị {{jsxref("NaN")}} của Number hay không. Vì, chỉ có NaN thật sự mới đưa ra được giá trị true khi mang vô phương thức này. Khác với global, miễn cái gì không phải là số thì cũng trả ra giá trị là true.

Examples

Number.isNaN(NaN);        // true
Number.isNaN(Number.NaN); // true
Number.isNaN(0 / 0)       // true

// e.g. these would have been true with global isNaN()
Number.isNaN("NaN");      // false
Number.isNaN(undefined);  // false
Number.isNaN({});         // false
Number.isNaN("blabla");   // false

// These all return false
Number.isNaN(true);
Number.isNaN(null);
Number.isNaN(37);
Number.isNaN("37");
Number.isNaN("37.37");
Number.isNaN("");
Number.isNaN(" ");

Polyfill

Number.isNaN = Number.isNaN || function(value) {
    return typeof value === "number" && isNaN(value);
}

// Or
Number.isNaN = Number.isNaN || function(value) {     
    return value !== value;
}

Specifications

Specification Status Comment
{{SpecName('ES6', '#sec-number.isnan', 'Number.isnan')}} {{Spec2('ES6')}} Initial definition.
{{SpecName('ESDraft', '#sec-number.isnan', 'Number.isnan')}} {{Spec2('ESDraft')}}  

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatChrome("25")}} {{CompatGeckoDesktop("15")}} {{CompatNo}} {{CompatVersionUnknown}} 9
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatNo}} {{CompatUnknown}} {{CompatGeckoMobile("15")}} {{CompatNo}} {{CompatNo}} 9

See also

  • {{jsxref("Number")}}
  • {{jsxref("isNaN", "isNaN()")}}

Revision Source

<div>{{JSRef}}</div>

<p>Phương thức&nbsp;<strong><code>Number.isNaN()</code></strong>&nbsp;(NaN viết tắt cho Not a Number)&nbsp;xác định xem giá trị đó có <em><strong>chính xác</strong></em>&nbsp;là&nbsp;{{jsxref("NaN")}} hay không. Một phiên bản khác của phương thức này, nhưng nằm trong phạm vi global đó là:&nbsp;{{jsxref("isNaN", "isNaN()")}}.</p>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">
<code>Number.isNaN(v<var>alue</var>)</code></pre>

<h3 id="Parameters">Parameters</h3>

<dl>
 <dt><code>value</code></dt>
 <dd>Giá trị truyền vô để kiểm tra xem nó có phải chính xác là&nbsp;{{jsxref("NaN")}} hay không.</dd>
</dl>

<h2 id="Description">Description</h2>

<p>Chả hai phương pháp operator so sánh bằng, đó là&nbsp;{{jsxref("Operators/Comparison_Operators", "==", "#Equality")}} và&nbsp;{{jsxref("Operators/Comparison_Operators", "===", "#Identity")}} đề trả ra&nbsp;<code>false</code> khi chúng ta kiểm tra giá trị&nbsp;{{jsxref("NaN")}}<em>là</em>&nbsp;{{jsxref("NaN")}} ví dụ: NaN === NaN, (đáng lẽ ra là true đúng không ?, nhưng thực tế giá trị NaN này không thể được xác định bằng cách này.),&nbsp;phương thức&nbsp;<code>Number.isNaN()</code>&nbsp;được tạo ra để làm việc này. Phương pháp này đưa ra 1 giải pháp để xác định NaN, NaN là một giá trị mà không thể so sánh với chính nó để kiểm tra như các giá trị khác trong JavaScript.</p>

<p>{{jsxref("isNaN", "isNaN()")}} là phương thức kiểm tra NaN trong phạm vi global, lưu ý là&nbsp;<code>Number.isNaN()</code>&nbsp;không giống như<code> isNaN() </code>trong global, <code>Number.isNaN()&nbsp;</code>xác định chính xác {{jsxref("NaN")}} đó có phải là giá trị {{jsxref("NaN")}}&nbsp;của Number hay không. Vì, chỉ có NaN thật sự mới đưa ra được giá trị true khi mang vô phương thức này. Khác với global, miễn cái gì không phải là số thì cũng trả ra giá trị là true.</p>

<h2 id="Examples">Examples</h2>

<pre class="brush: js">
Number.isNaN(NaN);        // true
Number.isNaN(Number.NaN); // true
Number.isNaN(0 / 0)       // true

// e.g. these would have been true with global isNaN()
Number.isNaN("NaN");      // false
Number.isNaN(undefined);  // false
Number.isNaN({});         // false
Number.isNaN("blabla");   // false

// These all return false
Number.isNaN(true);
Number.isNaN(null);
Number.isNaN(37);
Number.isNaN("37");
Number.isNaN("37.37");
Number.isNaN("");
Number.isNaN(" ");
</pre>

<h2 id="Polyfill">Polyfill</h2>

<pre class="brush: js">
Number.isNaN = Number.isNaN || function(value) {
&nbsp; &nbsp; return typeof value === "number" &amp;&amp; isNaN(value);
}

// Or
Number.isNaN = Number.isNaN || function(value) { &nbsp; &nbsp; 
&nbsp;   return value !== value;
}
</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-number.isnan', 'Number.isnan')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-number.isnan', 'Number.isnan')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<div>{{CompatibilityTable}}</div>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatChrome("25")}}</td>
   <td>{{CompatGeckoDesktop("15")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>9</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile("15")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>9</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{jsxref("Number")}}</li>
 <li>{{jsxref("isNaN", "isNaN()")}}</li>
</ul>
Revert to this revision