Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I believe the code is trying to ask, "is <code>x</code> numeric?" with the specific case here of <code>x = null</code>. The function <code>isNaN()</code> can be used to answer this question, but semantically it's referring specifically to the value <code>NaN</code>. From Wikipedia for <a href="http://en.wikipedia.org/wiki/NaN" rel="noreferrer"><code>NaN</code></a>: </p> <blockquote> <p>NaN (<b>N</b>ot <b>a</b> <b>N</b>umber) is a value of the numeric data type representing an undefined or unrepresentable value, especially in floating-point calculations.</p> </blockquote> <p>In most cases we think the answer to "is null numeric?" should be no. However, <code>isNaN(null) == false</code> is semantically correct, because <code>null</code> is not <code>NaN</code>.</p> <p>Here's the algorithmic explanation:</p> <p>The function <code>isNaN(x)</code> attempts to convert the passed parameter to a number<sup><a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/isNaN" rel="noreferrer">1</a></sup> (equivalent to <code>Number(x)</code>) and then tests if the value is <code>NaN</code>. If the parameter can't be converted to a number, <code>Number(x)</code> will return <code>NaN</code><sup><a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Number" rel="noreferrer">2</a></sup>. Therefore, if the conversion of parameter <code>x</code> to a number results in <code>NaN</code>, it returns true; otherwise, it returns false. </p> <p>So in the specific case <code>x = null</code>, <code>null</code> is converted to the number 0, (try evaluating <code>Number(null)</code> and see that it returns 0,) and <code>isNaN(0)</code> returns false. A string that is only digits can be converted to a number and isNaN also returns false. A string (e.g. <code>'abcd'</code>) that cannot be converted to a number will cause <code>isNaN('abcd')</code> to return true, specifically because <code>Number('abcd')</code> returns <code>NaN</code>.</p> <p>In addition to these apparent edge cases are the standard numerical reasons for returning NaN like 0/0.</p> <p>As for the seemingly inconsistent tests for equality shown in the question, the behavior of <code>NaN</code> is specified such that any comparison <code>x == NaN</code> is false, regardless of the other operand, including <code>NaN</code> itself<sup><a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/isNaN" rel="noreferrer">1</a></sup>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload