mozilla

Revision 309503 of Number.isNaN()

  • Revision slug: JavaScript/Reference/Global_Objects/Number/isNaN
  • Revision title: isNaN
  • Revision id: 309503
  • Created:
  • Creator: evilpie
  • Is current revision? No
  • Comment

Revision Content

{{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

 

Revision Source

<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>
<p><code>Number.isNaN(test<em>Value</em>)</code></p>
<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>
<p>&nbsp;</p>
Revert to this revision