[X]

{} + [] vs [] + {}

luljs still works

Swap the order of an empty object and an empty array and the answer flips from a string to zero.

In 2026: Still works, and it depends on context. As an expression, [] + {} is "[object Object]": both coerce to strings and join. But {} + [] typed as a statement is 0, because the parser reads the leading {} as an empty code block, not an object, leaving +[] which is 0. This asymmetry is the heart of Gary Bernhardt's Wat talk.

Where it came from: The star of Gary Bernhardt's four-minute Wat talk from CodeMash 2012. The flip is a parsing quirk: a leading {} is a block statement, not an object. Gary Bernhardt: Wat

<script>
// [] + {} as an expression: both coerce to strings and join.
document.write("<p><code>[] + {}</code> is <b>" + ([] + {}) + "</b></p>");
// {} + [] as a STATEMENT parses the leading {} as an empty code block,
// then runs +[] on its own, which is 0. Spelled out exactly as the parser sees it:
{}
var r = +[];
document.write("<p><code>{} + []</code> as a statement is <b>" + r + "</b></p>");
document.write("<p>The leading <code>{}</code> is an empty block, so only <code>+[]</code> runs, which is 0.</p>");
</script>
sandboxed demo · breaks nothing but itselfrestart

More in luljs

all 63 in luljs › · the whole library ›