Course contentsShow
← JavaScript
Lesson 290 of 2,7238. Type CoercionFree lesson

The {} + [] vs [] + {} Asymmetry

Why {} + [] can be 0 due to parsing ambiguity, while [] + {} is '[object Object]'.

The {} + [] vs [] + {} Asymmetry

What you'll learn: Why {} + [] and [] + {} produce shockingly different results in JavaScript.

The Puzzling Difference

When you write [] + {} in JavaScript, you get the string '[object Object]'. But when you flip it to {} + [], you often get 0. Wait, what? They should be symmetric, right?

Why the Asymmetry Exists

The culprit is parsing ambiguity.

When JavaScript sees {} + []:

  1. The parser can interpret the {} as an empty block statement (like an empty if-block), not an object literal
  2. That leaves + [] standing alone
  3. The unary + operator coerces [] to a number
  4. An empty array becomes 0
  5. Result: 0

When JavaScript sees [] + {}:

  1. It starts with [], which clearly begins an expression
  2. The + is definitely the addition operator
  3. Both operands coerce to strings: '' and '[object Object]'
  4. Result: '[object Object]'

The Context Matters

In a context where {} must be an expression (like inside parentheses or assigned to a variable), both behave consistently:

console.log({} + []);  // '[object Object]' — now it's an expression!
let result = {} + [];  // '[object Object]' — assignment forces expression

Key Takeaway: {} + [] often equals 0 because the parser treats {} as a code block rather than an object literal, leaving only + [] (unary plus on an empty array). Always use parentheses or assignment when you need consistent object literal behavior.