0

What is the difference between ({} + "") and {} + "" ?

Case 1: 
({} + "") \\ "[object Object]"
{} + "" \\ 0

Case 2:
({ a: 1 } + '') \\ "[object Object]"
{ a: 1 } + '' \\ 0

Case 3:
const obj = {};
(obj + '') \\ "[object Object]"
obj + '' \\ "[object Object]"

JavaScript is an amazing language!!!

geek machine
  • 173
  • 1
  • 2
  • 8
  • 1
    One of them is interpreted empty code block with `+""` and the other one is an empty object literal + "". [What is the explanation for these bizarre JavaScript behaviours mentioned in the 'Wat' talk for CodeMash 2012?](https://stackoverflow.com/questions/9032856) – adiga Dec 06 '20 at 08:01
  • What? `{ a: 1 }` is also an empty code block? @adiga – geek machine Dec 06 '20 at 08:20
  • 1
    It is interpreted as a code block with a [labeled statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label). If you try to add another property like `{ a: 1, b: 2 } + ''`, you should get an error because it's an invalid labeled statement: – adiga Dec 07 '20 at 05:38
  • Please check the duplicates at the top of the page for detailed explanations – adiga Dec 07 '20 at 08:05

0 Answers0