0

For some reason at least in the browsers console this is a valid expression which evaluates to x being equal to 1

{x} = {x:1}

However the same thing with a semicolon at the end throws the following error:

SyntaxError: Unexpected token =

Can anyone explain why?

eisbehr
  • 12,243
  • 7
  • 38
  • 63
Dave Demirkhanyan
  • 570
  • 1
  • 5
  • 22

1 Answers1

0

You just need to write var in front:

var {x} = {x:2};
console.log(x);
eisbehr
  • 12,243
  • 7
  • 38
  • 63
  • I know that this is not throwing an error and this is a correct way to assign value to x in this case, I am wondering why a lack of ```var``` or ```let``` in the same statement with a semicolon throws an error, and the same statement without one doesn't. – Dave Demirkhanyan May 07 '18 at 11:06
  • 1
    @DaveDemirkhanyan The spec might require `var` or `let` to destructure. It might lead to ambiguous cases otherwise, or something. It might make parsing more difficult too, since you'd have to get passed the destructure braces to see if it's defining literal or destructuring and object. – Carcigenicate May 07 '18 at 11:16