Yes, {} + [] = 0. No, JavaScript Isn’t Broken
Post · https://dev.to/sylwia-lask/yes-0-no-javascript-isnt-broken-14ob
JavaScript is a legendary programming language. Created in the mid-1990s in just 10 days, it was born...
First seen by Oriel on .
AI summary
This JavaScript WTF article explains how parsing rules and type coercion make {} + [] evaluate to 0 without the language being broken.
- At the start of a classic-script statement, JavaScript can parse {} as an empty block and [] as the operand of unary plus.
- Unary plus converts [] through [].toString(), producing an empty string, which Number("") converts to 0.
- Parentheses or function-call context force {} to be an object, making ({} + []) and console.log({} + []) produce "[object Object]".
- [] + [] produces an empty string, while [] + {} produces "[object Object]" because both operands undergo string conversion.
- These results follow parsing rules, implicit type conversion, and backward compatibility rather than representing JavaScript bugs.
#javascript #type coercion #parsing #backward compatibility
Generated by gpt-5.6-sol. Check the original for details.
Metric trends
Last 30 days · Up to 120 recorded samples per source and metric, with reposts kept separate · First and latest retained
Reactions
Total public reactions reported by the source; distinct from points and likes.
- DEV Community: 170, latest observation ; +154 over observed period since
Comments
Comment count reported by the source.
- DEV Community: 126, latest observation ; +121 over observed period since
Aliases
| Source | External ID | URL |
|---|---|---|
| DEV Community | 4943245 | https://dev.to/sylwia-lask/yes-0-no-javascript-isnt-broken-14ob Read discussion |