MATTHEWLAB
Software engineering

Before debugging the value, check the context.

Why the same JavaScript characters can produce different results—and what to preserve in a useful reproduction.

Brackets, an empty frame, and a magnifying glass illustrating parsing context.

The expression looks like a joke: add an empty object to an empty array and get zero. Put parentheses around it and the result changes. The useful lesson is not that JavaScript has abandoned its rules. It is that the parser has to decide what those characters mean before a value can be calculated.

Preserve the original input

At the beginning of a Script, {} can be parsed as an empty block. The remaining +[] is unary plus applied to an empty array. In ({} + []), the braces instead belong to an object expression.

These results were checked in Node.js v26.0.0 using vm.runInNewContext, with each input evaluated as a separate Script:

InputResultType
{} + []0number
({} + [])"[object Object]"string
[] + []""string
[] + {}"[object Object]"string

A developer console may treat an input differently from a Script file. The table is evidence for this specific runtime and evaluation method, not a promise about every console.

Separate parsing from conversion

MDN’s block statement reference explains blocks; its addition reference explains the operator’s numeric and string behavior. Those are separate questions. First establish the syntax being evaluated, then follow the relevant conversion rules.

Adding parentheses immediately can hide the original condition. Keep the original input and record the parenthesized variant as a second case. Change one condition at a time.

A reproduction needs more than a screenshot

Record the exact input, runtime version, evaluation context, output value, and output type. “It returned zero” leaves out whether that was the number 0 or the string "0", and whether the tool wrapped the input as an expression.

Then return to the actual product decision. If a price field is missing, automatic coercion should not decide whether the application treats it as zero or rejects the input. That is a rule the application has to make explicit.

한국어 글: 자바스크립트에서 {} + []가 0이 되는 이유