Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a list of explanations for the results you're seeing (and supposed to be seeing). The references I'm using are from the <a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm">ECMA-262 standard</a>.</p> <ol> <li><h3><code>[] + []</code></h3> <p>When using the addition operator, both the left and right operands are converted to primitives first (<a href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.6.1">§11.6.1</a>). As per <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-9.1">§9.1</a>, converting an object (in this case an array) to a primitive returns its default value, which for objects with a valid <code>toString()</code> method is the result of calling <code>object.toString()</code> (<a href="http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.8">§8.12.8</a>). For arrays this is the same as calling <code>array.join()</code> (<a href="http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.2">§15.4.4.2</a>). Joining an empty array results in an empty string, so step #7 of the addition operator returns the concatenation of two empty strings, which is the empty string.</p></li> <li><h3><code>[] + {}</code></h3> <p>Similar to <code>[] + []</code>, both operands are converted to primitives first. For "Object objects" (§15.2), this is again the result of calling <code>object.toString()</code>, which for non-null, non-undefined objects is <code>"[object Object]"</code> (<a href="http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.4.2">§15.2.4.2</a>).</p></li> <li><h3><code>{} + []</code></h3> <p>The <code>{}</code> here is not parsed as an object, but instead as an empty block (<a href="http://www.ecma-international.org/ecma-262/5.1/#sec-12.1">§12.1</a>, at least as long as you're not forcing that statement to be an expression, but more about that later). The return value of empty blocks is empty, so the result of that statement is the same as <code>+[]</code>. The unary <code>+</code> operator (<a href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.4.6">§11.4.6</a>) returns <code>ToNumber(ToPrimitive(operand))</code>. As we already know, <code>ToPrimitive([])</code> is the empty string, and according to <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-9.3.1">§9.3.1</a>, <code>ToNumber("")</code> is 0.</p></li> <li><h3><code>{} + {}</code></h3> <p>Similar to the previous case, the first <code>{}</code> is parsed as a block with empty return value. Again, <code>+{}</code> is the same as <code>ToNumber(ToPrimitive({}))</code>, and <code>ToPrimitive({})</code> is <code>"[object Object]"</code> (see <code>[] + {}</code>). So to get the result of <code>+{}</code>, we have to apply <code>ToNumber</code> on the string <code>"[object Object]"</code>. When following the steps from <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-9.3.1">§9.3.1</a>, we get <code>NaN</code> as a result:</p> <blockquote> <p>If the grammar cannot interpret the String as an expansion of <em>StringNumericLiteral</em>, then the result of <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-9.3">ToNumber</a> is <strong>NaN</strong>.</p> </blockquote></li> <li><h3><code>Array(16).join("wat" - 1)</code></h3> <p>As per <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.1.1">§15.4.1.1</a> and <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.2.2">§15.4.2.2</a>, <code>Array(16)</code> creates a new array with length 16. To get the value of the argument to join, <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.6.2">§11.6.2</a> steps #5 and #6 show that we have to convert both operands to a number using <code>ToNumber</code>. <code>ToNumber(1)</code> is simply 1 (<a href="http://www.ecma-international.org/ecma-262/5.1/#sec-9.3">§9.3</a>), whereas <code>ToNumber("wat")</code> again is <code>NaN</code> as per <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-9.3.1">§9.3.1</a>. Following step 7 of <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.6.2">§11.6.2</a>, <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.6.3">§11.6.3</a> dictates that </p> <blockquote> <p>If either operand is <strong>NaN</strong>, the result is <strong>NaN</strong>.</p> </blockquote> <p>So the argument to <code>Array(16).join</code> is <code>NaN</code>. Following §15.4.4.5 (<code>Array.prototype.join</code>), we have to call <code>ToString</code> on the argument, which is <code>"NaN"</code> (<a href="http://www.ecma-international.org/ecma-262/5.1/#sec-9.8.1">§9.8.1</a>):</p> <blockquote> <p>If <em>m</em> is <strong>NaN</strong>, return the String <strong><code>"NaN"</code></strong>.</p> </blockquote> <p>Following step 10 of <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.5">§15.4.4.5</a>, we get 15 repetitions of the concatenation of <code>"NaN"</code> and the empty string, which equals the result you're seeing. When using <code>"wat" + 1</code> instead of <code>"wat" - 1</code> as argument, the addition operator converts <code>1</code> to a string instead of converting <code>"wat"</code> to a number, so it effectively calls <code>Array(16).join("wat1")</code>.</p></li> </ol> <p>As to why you're seeing different results for the <code>{} + []</code> case: When using it as a function argument, you're forcing the statement to be an <em>ExpressionStatement</em>, which makes it impossible to parse <code>{}</code> as empty block, so it's instead parsed as an empty object literal.</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.
    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