Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommend Number.isNaN(), not global isNan() #3135

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN#description">which behaves just as its name implies</a>:</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, <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN#confusing_special-case_behavior">which has unintuitive behavior</a>:</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