mozilla

Revision 372177 of isNaN()

  • Revision slug: JavaScript/Reference/Global_Objects/isNaN
  • Revision title: isNaN
  • Revision id: 372177
  • Created:
  • Creator: dbruant
  • Is current revision? No
  • Comment
Tags: 

Revision Content

Summary

Determines whether a value is NaN or not. Be careful, this function is broken. You may be interested in ECMAScript 6 Number.isNaN.

Core Global Method
Implemented in JavaScript 1.0
ECMAScript Edition ECMAScript 1st Edition

Syntax

isNaN(testValue)

Parameters

testValue
The value to be tested.

Description

The necessity of an isNaN function

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 an isNaN function.

Origin of NaN values

NaN values are generated when arithmetic operations result in undefined or unrepresentable values. Such values do not necessarily represent overflow conditions. A NaN also results from attempted coercion to numeric values of non-numeric values for which no primitive numeric value is available.

For example, dividing zero by zero results in a NaN — but dividing other numbers by zero does not.

Confusing special-case behavior

Since the very earliest versions of the isNaN function specification, its behavior for non-numeric arguments has been confusing. When the argument to the isNaN function is not a value of type Number, the value is first coerced to a number. The resulting value is then tested to determine whether it is NaN. Thus for non-numbers that when coerced to numeric type result in a valid non-NaN numeric value (notably the empty string and boolean primitives, which when coerced give numeric values zero or one), the "false" returned value may be unexpected; the empty string, for example, is surely "not a number." The confusion stems from the fact that the term, "not a number", has a specific meaning for numbers represented as IEEE-794 floating-point values. The function should be interpreted as answering the question, "is this value, when coerced to a numeric value, an IEEE-794 'Not A Number' value?"

The next version of ECMAScript (ES6) contains the function Number.isNaN function. Number.isNaN(x) will be a reliable way to test whether x is NaN or not. Even with Number.isNaN, however, the meaning of NaN remains the precise numeric meaning, and not simply, "not a number". Alternatively, in absense of Number.isNaN, the expression (x != x) is a more reliable way to test whether variable x is NaN or not, as the result is not subject to the false positives that make isNaN unreliable.

Examples

isNaN(NaN);       // true
isNaN(undefined); // true
isNaN({});        // true

isNaN(true);      // false
isNaN(null);      // false
isNaN(37);        // false

// strings
isNaN("37");      // false: "37" is converted to the number 37 which is not NaN
isNaN("37.37");   // false: "37.37" is converted to the number 37.37 which is not NaN
isNaN("");        // false: the empty string is converted to 0 which is not NaN
isNaN(" ");       // false: a string with spaces is converted to 0 which is not NaN

// This is a false positive and the reason why isNaN is not entirely reliable
isNaN("blabla")   // true: "blabla" is converted to a number. Parsing this as a number fails and returns NaN

See also

Revision Source

<h2 id="Summary" name="Summary">Summary</h2>
<p>Determines whether a value is <a href="/en-US/docs/JavaScript/Reference/Global_Objects/NaN" title="JavaScript/Reference/Global_Objects/NaN">NaN</a> or not. Be careful, this <a href="#Description">function is broken</a>. You may be interested in ECMAScript 6 <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Number/isNaN" title="/en-US/docs/JavaScript/Reference/Global_Objects/Number/isNaN">Number.isNaN</a>.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Core Global Method</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.0</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>ECMAScript 1st Edition</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code>isNaN(<em>testValue</em>)</code></pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<dl>
  <dt>
    <code>testValue</code></dt>
  <dd>
    The value to be tested.</dd>
</dl>
<h2 id="Description" name="Description">Description</h2>
<h3 id="The_necessity_of_an_isNaN_function" name="The_necessity_of_an_isNaN_function">The necessity of an <code>isNaN</code> function</h3>
<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="/en-US/docs/JavaScript/Reference/Global_Objects/NaN" title="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 an <code>isNaN</code> function.</p>
<h3 id="Origin_of_NaN_values" name="Origin_of_NaN_values">Origin of <code>NaN</code> values</h3>
<p><code>NaN</code> values are generated when arithmetic operations result in <em>undefined</em> or <em>unrepresentable</em> values. Such values do not necessarily represent overflow conditions. A <code>NaN</code> also results from attempted coercion to numeric values of non-numeric values for which no primitive numeric value is available.</p>
<p>For example, dividing zero by zero results in a <code>NaN</code> — but dividing other numbers by zero does not.</p>
<h3 id="Confusing_special-case_behavior">Confusing special-case behavior</h3>
<p>Since the very earliest versions of the <code>isNaN</code> function specification, its behavior for non-numeric arguments has been confusing. When the argument to the <code>isNaN</code> function is not a value of type Number, the value is first coerced to a number. The resulting value is then tested to determine whether it is <code><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Number/NaN" title="JavaScript/Reference/Global_Objects/Number/NaN">NaN</a></code>. Thus for non-numbers that when coerced to numeric type result in a valid non-NaN numeric value (notably the empty string and boolean primitives, which when coerced give numeric values zero or one), the "false" returned value may be unexpected; the empty string, for example, is surely "not a number." The confusion stems from the fact that the term, "not a number", has a specific meaning for numbers represented as IEEE-794 floating-point values. The function should be interpreted as answering the question, "is this value, when coerced to a numeric value, an IEEE-794 'Not A Number' value?"</p>
<p>The next version of ECMAScript (ES6) contains the function <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Number/isNaN" title="JavaScript/Reference/Global_Objects/Number/isNaN">Number.isNaN</a> function. <code>Number.isNaN(x)</code> will be a reliable way to test whether <code>x</code> is <code>NaN</code> or not. Even with <code>Number.isNaN</code>, however, the meaning of <code>NaN</code> remains the precise numeric meaning, and not simply, "not a number". Alternatively, in absense of <code>Number.isNaN</code>, the expression <code>(x != x)</code> is a more reliable way to test whether variable <code>x</code> is <code>NaN</code> or not, as the result is not subject to the false positives that make <code>isNaN</code> unreliable.</p>
<h2 id="Examples" name="Examples">Examples</h2>
<pre class="brush: js">
isNaN(NaN);       // true
isNaN(undefined); // true
isNaN({});        // true

isNaN(true);      // false
isNaN(null);      // false
isNaN(37);        // false

// strings
isNaN("37");      // false: "37" is converted to the number 37 which is not NaN
isNaN("37.37");   // false: "37.37" is converted to the number 37.37 which is not NaN
isNaN("");        // false: the empty string is converted to 0 which is not NaN
isNaN(" ");       // false: a string with spaces is converted to 0 which is not NaN

// This is a false positive and the reason why isNaN is not entirely reliable
isNaN("blabla")   // true: "blabla" is converted to a number. Parsing this as a number fails and returns NaN
</pre>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Number/NaN" title="JavaScript/Reference/Global_Objects/Number/NaN">NaN</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Number/isNaN" title="JavaScript/Reference/Global_Objects/Number/isNaN">Number.isNaN</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/parseFloat" title="JavaScript/Reference/Global_Functions/parseFloat">parseFloat</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/parseInt" title="JavaScript/Reference/Global_Functions/parseInt">parseInt</a></li>
  <li><a href="http://es5.github.com/#x15.1.2.4">(unofficial) web-based ECMAScript 5.1 isNaN specification</a></li>
</ul>
Revert to this revision