0

I learn destructuring and I was just get an error because I didn't use semicolon on "obj" object

let a = 11
let b = 989
const obj = {a:23, b:7, c:14};
({a, b} = obj)
console.log(a,b)

This is only work if I use semicolon. If I don't use it I get an initialization error and I am totally confused about this. Can anyone explain me why this is happen?

Maytham Fahmi
  • 26,353
  • 12
  • 100
  • 121
  • 4
    Using semicolons is always a good idea. Otherwise Javascript tries to guess where they should be and, as you can see, doesn't always get it quite right. At least you got an error message rather than some subtle wrong behavior. – Vilx- Apr 25 '21 at 17:35
  • 1
    [Everything you need to know about JavaScript Semicolon Insertion](https://web.archive.org/web/20201206065632/http://inimino.org/~inimino/blog/javascript_semicolons) – Bergi Apr 25 '21 at 17:38
  • 1
    Semicolons are optional, in general -- but without them, the compiler will try to guess what two lines mean in succession, and won't always guess correctly. In this case, its seeing the `(...)` on line 4 as a function parameter list, and compiling as though the previous `{...}` is a function being called. – Paul Roub Apr 25 '21 at 17:38
  • Okay, now that makes sense thank you so much. – utkuonursahin Apr 25 '21 at 17:43

0 Answers0