Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2><code>if ({}) alert('true')</code> -> <code>true</code></h2> <p><code>{}</code> is an object, which, when evaluated in the context of an <code>if</code> statement, gets coerced to a <code>Boolean</code>, and since <code>Boolean({})</code> evaluates to <strong><code>true</code></strong>, you get <code>if (true)</code>. This is documented in the ECMAScript specification, Section <strong><a href="http://ecma-international.org/ecma-262/5.1/#sec-12.5" rel="nofollow">12.5 The if Statement</a></strong>:</p> <blockquote> <p>The production If Statement : if ( Expression ) Statement is evaluated as follows:</p> <ol> <li>Let exprRef be the result of evaluating Expression. </li> <li>If <strong>ToBoolean(GetValue(exprRef))</strong> is false, return (normal, empty, empty).</li> <li>Return the result of evaluating Statement.</li> </ol> </blockquote> <h2><code>alert({} == true)</code> -> <code>false</code></h2> <p>This one is more tricky. From the <a href="http://en.wikipedia.org/wiki/ECMAScript" rel="nofollow">ECMAScript</a> specification, Section <strong><a href="http://ecma-international.org/ecma-262/5.1/#sec-11.9.3" rel="nofollow">11.9.3 The Abstract Equality Comparison Algorithm</a></strong>:</p> <blockquote> <p>The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:</p> <p>If Type(y) is Boolean, return the result of the comparison <strong>x == ToNumber(y)</strong>.</p> </blockquote> <p>Thus, <code>{} == true</code> will be evaluated as <code>{} == Number(true)</code>, which is evaluated to <code>{} == 1</code>, which is <strong><code>false</code></strong>.</p> <p>This is also why <code>1 == true</code> evaluates to <code>true</code>, but <code>2 == true</code> evaluates to <code>false</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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