Skip to content

Commit

Permalink
Recommend Number.isNaN(), not global isNan()
Browse files Browse the repository at this point in the history
Fixes #3026
  • Loading branch information
sideshowbarker committed Mar 15, 2021
1 parent bb8c53c commit 1ce2211
Showing 1 changed file with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,25 @@ <h2 id="Numbers">Numbers</h2>
<pre class="brush: js">NaN + 5; // NaN
</pre>

<p>You can test for <code>NaN</code> using the built-in {{jsxref("Global_Objects/isNaN", "isNaN()")}} function:</p>
<p>You can reliably test for <code>NaN</code> using the built-in {{jsxref("Number.isNaN", "Number.isNaN()")}} function:</p>

<pre class="brush: js">isNaN(NaN); // true
<pre class="brush: js">Number.isNaN(NaN); // true
Number.isNaN('hello'); // false
Number.isNaN('1'); // false
Number.isNaN(undefined); // false
Number.isNaN({}); // false
Number.isNaN([1]) // false
Number.isNaN([1,2]) // false
</pre>

<p>But don’t test for <code>NaN</code> using the global {{jsxref("Global_Objects/isNaN", "isNaN()")}} function, which has unintuitive behavior:</p>

<pre class="brush: js">isNaN('hello'); // true
isNaN('1'); // false
isNaN(undefined); // true
isNaN({}); // true
isNaN([1]) // false
isNaN([1,2]) // true
</pre>

<p>JavaScript also has the special values {{jsxref("Infinity")}} and <code>-Infinity</code>:</p>
Expand Down

0 comments on commit 1ce2211

Please sign in to comment.