Apply your JS skills to key Mozilla projects as an MDN Fellow! http://mzl.la/MDNFellowship

mozilla

Revision 358247 of Number.isNaN()

  • Revision slug: JavaScript/Reference/Global_Objects/Number/isNaN
  • Revision title: isNaN
  • Revision id: 358247
  • Created:
  • Creator: m_gol
  • Is current revision? No
  • Comment a typo correction

Revision Content

{{harmony}}

{{fx_minversion_header("15") }}

Summary

Determine whether the passed value is NaN. Better version of the global isNaN.

Method of Number
Implemented in JavaScript 1.8.5+
ECMAScript Edition ECMAScript 6th Edition

Syntax

Number.isNaN(testValue)

Parameters

testValue
The value to be tested for NaN.

Description

Unlike all other possible values in JavaScript, it is not possible to rely on the equality operators (== and ===) to determine whether a value is NaN or not, because both NaN == NaN and NaN === NaN evaluate to false. Hence, the necessity of the Number.isNaN function.

In comparison to the globabl isNaN function, this method doesn't suffer the problem of forcefully converting the parameter to a number. This means it is safe to pass even values that would convert to NaN, but aren't actually the same value as NaN. This means only values of the type number, that are also NaN, return true.

Examples

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

// everything else: false
Number.isNaN(undefined);
Number.isNaN({});     

Number.isNaN(true);
Number.isNaN(null);
Number.isNaN(37);

Number.isNaN("37");
Number.isNaN("37.37");
Number.isNaN("");
Number.isNaN(" ");
Number.isNaN("NaN");
Number.isNaN("blabla"); // e.g. this would have been true with isNaN

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 25.0 {{ CompatGeckoDesktop("15") }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatNo() }} {{ CompatGeckoDesktop("15") }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}

 

Revision Source

<div>
  {{harmony}}</div>
<p>{{fx_minversion_header("15") }}</p>
<h2 id="Summary">Summary</h2>
<p>Determine whether the passed value is <a href="/en-US/docs/JavaScript/Reference/Global_Objects/NaN" title="/en-US/docs/JavaScript/Reference/Global_Objects/NaN">NaN</a>. Better version of the global <a href="/en-US/docs/JavaScript/Reference/Global_Objects/isNaN" title="/en-US/docs/JavaScript/Reference/Global_Objects/isNaN">isNaN</a>.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of<code> <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Number" title="/en-US/docs/JavaScript/Reference/Global_Objects/Number">Number</a></code></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.8.5+</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>ECMAScript 6th Edition</td>
    </tr>
  </tbody>
</table>
<h3 id="Syntax" name="Syntax">Syntax</h3>
<pre class="syntaxbox">
<code>Number.isNaN(test<em>Value</em>)</code></pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<dl>
  <dt>
    <code>test<em>Value</em></code></dt>
  <dd>
    The value to be tested for <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Number/NaN" title="/en-US/docs/JavaScript/Reference/Global_Objects/Number/NaN">NaN</a>.</dd>
</dl>
<h2 id="Description">Description</h2>
<p>Unlike all other possible values in JavaScript, it is not possible to rely on the equality operators (== and ===) to determine whether a value <em>is</em> <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/NaN" title="en-US/docs/JavaScript/Reference/Global_Objects/NaN">NaN</a> or not, because both <code>NaN == NaN</code> and <code>NaN === NaN</code> evaluate to <code>false</code>. Hence, the necessity of the <code>Number.isNaN</code> function.</p>
<p>In comparison to the globabl <a href="/en-US/docs/JavaScript/Reference/Global_Objects/isNaN" title="/en-US/docs/JavaScript/Reference/Global_Objects/isNaN">isNaN</a> function, this method doesn't suffer the problem of forcefully converting the parameter to a number. This means it is safe to pass even values that would convert to NaN, but aren't actually the same value as NaN. This means only values of the type number, that are also NaN, return <code>true</code>.</p>
<h2 id="Examples">Examples</h2>
<pre class="brush:js;">
Number.isNaN(NaN); // true
Number.isNaN(Number.NaN); // true
Number.isNaN(0 / 0) // true

// everything else: false
Number.isNaN(undefined);
Number.isNaN({});&nbsp;&nbsp;&nbsp;&nbsp; 

Number.isNaN(true);
Number.isNaN(null);
Number.isNaN(37);

Number.isNaN("37");
Number.isNaN("37.37");
Number.isNaN("");
Number.isNaN(" ");
Number.isNaN("NaN");
Number.isNaN("blabla"); // e.g. this would have been true with isNaN</pre>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<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>25.0</td>
        <td>{{ CompatGeckoDesktop("15") }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>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>{{ CompatGeckoDesktop("15") }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
      </tr>
    </tbody>
  </table>
</div>
<p>&nbsp;</p>
Revert to this revision