Skip to content
ARGBE.TECH

Article in English · Deutsch

, 4 min readLast checked By Aleksandr Argunov

JavaScript's {} + [] gives 0 at statement start. Leave it.

At the start of a statement, {} is an empty block and the rest is array coercion. Six cases you can run in Node, a logging trap, and why I would leave it alone.

{} + [] is 0 when it starts a JavaScript statement. I never need this. Especially now. It’s an empty block followed by array coercion. As an expression, the same text gives a string. Node’s vm module shows the difference.

The idea comes from Sylwia Laskowska’s DEV article, which explains the block and the coercion.

Museum hall titled Not a bug. A giant steel sum, empty braces plus empty brackets, equals zero. Neon object Object text, an empty gilded frame and a banana taped to the wall.
Embed this GIF
<a href="https://argbe.tech/blog/javascript-empty-block-array-coercion"><img src="https://argbe.tech/media/javascript-empty-block-array-coercion.gif" alt="Museum hall titled Not a bug. A giant steel sum, empty braces plus empty brackets, equals zero. Neon object Object text, an empty gilded frame and a banana taped to the wall." width="800" height="1000"></a>

The taped banana is a nod to Maurizio Cattelan’s Comedian (2019).

JavaScript empty blocks and array coercion: statement vs expression

Paste this into a terminal to compare them with Node’s vm:

node - <<'JS'
const vm = require('node:vm')
const cases = ['{} + []', '({} + [])', '[] + {}', '[] + []', '{} + {}', '({} + {})']
for (const code of cases) {
  const value = vm.runInNewContext(code)
  const shown = typeof value === 'string' ? JSON.stringify(value) : String(value)
  console.log(`${code} => ${shown} (${typeof value})`)
}
JS

On Node v26.13.0 you get this:

{} + [] => 0 (number)
({} + []) => "[object Object]" (string)
[] + {} => "[object Object]" (string)
[] + [] => "" (string)
{} + {} => NaN (number)
({} + {}) => "[object Object][object Object]" (string)

vm.runInNewContext runs each string as a script and returns its result. Node’s docs also say vm is not a security mechanism. Use it on your own strings, like these.

CodeWhat the parser seesResult
{} + []Empty block, then +[]0
({} + [])Object plus array"[object Object]"
[] + {}Array plus object"[object Object]"
[] + []Array plus array""
{} + {}Empty block, then +{}NaN
({} + {})Object plus object"[object Object][object Object]"

The rule is in the grammar. ECMA-262 excludes an opening { from an expression statement. Here it opens an empty block instead. That leaves +[] or +{}.

The parenthesised cases and the rows starting with [] use binary addition. In these examples, both operands become strings, which concatenate.

Why +[] is 0

node - <<'JS'
console.log(JSON.stringify(String([])))
console.log(Number(''))
console.log(+[])
console.log(String({}))
console.log(+{})
JS
""
0
0
[object Object]
NaN

Unary plus converts to a number. An empty array becomes an empty string. An empty string becomes 0. The plain object here becomes [object Object]. Converting that string to a number gives NaN.

Same characters, different answer

node -p '{} + []'
node -e 'console.log({} + [])'
printf '%s\n' '{} + []' | node -i
0
[object Object]
Welcome to Node.js v26.13.0.
Type ".help" for more information.
> '[object Object]'
>

node -p treats the line as a script. Inside console.log(...) it’s an argument, so an expression, and {} is an object. The REPL’s answer matches the parenthesised form.

The Node REPL and a script give different answers here. Browser DevTools aren’t covered by these examples.

Where [object Object] shows up in logs

I’ve seen [object Object] in console.log, when I couldn’t read it while debugging. I don’t remember exactly, but also GraphQL and React.

When debugging software, the way you pass an object to console.log matters:

node - <<'JS'
const user = { id: 7 }
console.log(user)
console.log('user: ' + user)
console.log(`user: ${user}`)
console.log('user:', user)
console.log('user: ' + JSON.stringify(user))
JS
{ id: 7 }
user: [object Object]
user: [object Object]
user: { id: 7 }
user: {"id":7}

Use a comma, not a plus. Node logs the object as a separate argument.

JSON.stringify has its own trap:

node - <<'JS'
console.log(JSON.stringify(NaN))
console.log(JSON.stringify({ total: NaN }))
console.log(String(NaN), typeof NaN)
JS
null
{"total":null}
NaN number

That’s why the first script prints String(value) and typeof. With JSON alone, the NaN row would read null. A snapshot of that string can pass with a broken total, and a passing test doesn’t prove it checks anything. JSON changes numbers in other ways too: a long ID can come out of JSON.parse as a different number.

I’d leave {} + [] alone

Sylwia frames this as a compatibility question: existing sites still use old JavaScript behaviour.

Would I fix {} + [] if some old websites broke? No, leave it. Nobody cares nowadays.