3
>{a} = {a: true} // Statement
<{a: true}

The above statement is assigning the value true to a

Why is the above statement not giving error in the chrome console? While the below statement gives an error.

>{a} = {a: true}; // Statement
<Uncaught SyntaxError: Unexpected token '='

How does the semicolon at the end matter?

  • Without the semicolon, when parsing, Javascript engine considers a line as a single call expression and invoke it. When you added a semicolon it means that you defining a variable, so you need to add `const` or `var` on the beginning. – demkovych Jun 25 '20 at 09:59
  • It is interpreting the `{ a }` as a block. I assumed it would throw an error for the first one as well without a `()` wrapper. – adiga Jun 25 '20 at 09:59
  • you need to declare `a`. like `var {a}` – bill.gates Jun 25 '20 at 10:00
  • @Ifaruki Why is first one not throwing an error? – adiga Jun 25 '20 at 10:00
  • 1
    @adiga it wil throw an error – ABGR Jun 25 '20 at 10:01
  • @ABGR It's only working in Chrome console. It's actually an invalid expression on it's own without the `()` wrapper around the destructuring. So, something to do with Chrome's console – adiga Jun 25 '20 at 10:09
  • 1
    From [this answer](https://stackoverflow.com/a/36438289/3082296): *"Chrome devtools now automatically wraps everything that begins with `{` and ends with `}` in an implicit pair of parentheses*". So, the first block is converted to `({ a } = { a: true })` while the second one isn't. – adiga Jun 25 '20 at 10:20
  • @adiga it's actually V8 itself. A Node.js REPL, an Opera console and others that use V8 also produce the same behaviour. – VLAZ Jun 25 '20 at 10:22
  • @VLAZ yes, the second duplicate in the list mentions Node and V8. – adiga Jun 25 '20 at 10:28

0 Answers0