Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your real question seem to be:</p> <p>Why:</p> <pre><code>null &gt;= 0; // true </code></pre> <p>But:</p> <pre><code>null == 0; // false </code></pre> <p>What really happens is that the <em>Greater-than-or-equal Operator</em> (<code>&gt;=</code>), performs type coercion (<a href="http://www.ecma-international.org/ecma-262/5.1/#sec-9.1" rel="noreferrer"><code>ToPrimitive</code></a>), with a <em>hint</em> type of <code>Number</code>, actually all the relational operators have this behavior.</p> <p><code>null</code> is treated in a special way by the <em>Equals Operator</em> (<code>==</code>). In a brief, it only <em>coerces</em> to <code>undefined</code>:</p> <pre><code>null == null; // true null == undefined; // true </code></pre> <p>Value such as <code>false</code>, <code>''</code>, <code>'0'</code>, and <code>[]</code> are subject to numeric type coercion, all of them coerce to zero.</p> <p>You can see the inner details of this process in the <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3" rel="noreferrer">The Abstract Equality Comparison Algorithm</a> and <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.8.5" rel="noreferrer">The Abstract Relational Comparison Algorithm</a>.</p> <p>In Summary: </p> <ul> <li><p>Relational Comparison: if both values are not type String, <code>ToNumber</code> is called on both. This is the same as adding a <code>+</code> in front, which for null coerces to <code>0</code>. </p></li> <li><p>Equality Comparison: only calls <code>ToNumber</code> on Strings, Numbers, and Booleans.</p></li> </ul>
 

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