2

This code executes perfectly in the google chrome console:

{ a, b } = { a: 10, b: 20 }

{a: 10, b: 20}

But when I add semicolon at the end, I get syntax error:

{ a, b } = { a: 10, b: 20 };

VM115:1 Uncaught SyntaxError: Unexpected token '='

Can someone tell me why? AFAIK ; should not affect the meaning in javascript but apparently it does in google chrome console.

P.S. Please pay attention that this behavior is only observed in Google Chrome console.

yukashima huksay
  • 5,834
  • 7
  • 45
  • 78
Pegtomous
  • 79
  • 6
  • 2
    Both these are syntax errors for me (firefox JS console). Semicolon does not, indeed, make a difference. – Sergio Tulentsev May 20 '21 at 13:04
  • @Quentin I wanted to write an answer, which got converted into a comment for being trivial, so I deleted the comment and wrote an answer that's one sentence longer. Do you think we should dupe-close this? – Bergi May 20 '21 at 13:12
  • 2
    Duplicate of [Object destructuring in JavaScript](https://stackoverflow.com/questions/62572662) and [Having semicolon after spread syntax jn javascript breaks execution with error “Unexpected token =”](https://stackoverflow.com/questions/51003328) and [Why is {} + {} no longer NaN in Chrome console?](https://stackoverflow.com/questions/36438034) and [Node JS / V8 destructuring bug?](https://stackoverflow.com/questions/46103157) – adiga May 20 '21 at 13:13
  • @Bergi — It does seem to be a pretty close match. Possibly sufficiently different to justify its own question though. – Quentin May 20 '21 at 13:14
  • @adiga Ah, the "*Node JS / V8 destructuring bug?*" one seems to be a perfect duplicate. I hadn't known that – Bergi May 20 '21 at 13:17

1 Answers1

6

object destructuring without var is only valid with parenthesis around the assignment expression - a statement must not begin with { in JavaScript, it would be a block otherwise.

The behaviour you experience comes from the special handling of inputs that begin with { and end with } in the Chrome devtools. This heuristic is sometimes useful, but causes lots of confusion in cases where it goes wrong.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375