0

I am simply using es6 to reassign variables a & b. Why do I get an error if I leave the semicolon off of the a and b declaration and assignment statements? Does the parser try to pull a property out of 2 if the semicolon is left off? let b = 2[a, b]...?

Works:
let a = 1;
let b = 2;
[a, b] = [b, a]

Error:

let a = 1
let b = 2
[a, b] = [b, a]

I am looking for the actual reason this fails. Thanks!

j08691
  • 204,283
  • 31
  • 260
  • 272
daniel
  • 61
  • 1
  • 1
  • 2

1 Answers1

1

Does the parser try to pull a property out of 2 if the semicolon is left off? let b = 2[a, b]...?

Yes. You cannot safely start a statement with a [ if you're not explicitly ending statements with semicolons.

This has been written about in many places, and it's one of the many things that standardJS (or other linters) will warn you about: http://standardjs.com/rules.html#semicolons

user229044
  • 232,980
  • 40
  • 330
  • 338
  • 1
    @brso05 This is because it is parsed as `let b = 2[a, b] = [b, a]`, meaning you're trying to reference `b` *inside* the definition of `b`. – user229044 Sep 28 '16 at 17:17