DEV Community

Discussion on: Destructuring Assignment in ES6- Arrays

Collapse
 
jwm63ara profile image
jwm63ara

Hi Anya,

I'm still learning too, so I don't fully understand this, but it is an example of a situation where automatic semicolon insertion doesn't work the way you might expect. If you try

var a = 3
var b = 7;
[a, b] = [b, a]

you should find that you get the expected result. The following also works:

var a = 3
var b = 7
{
  [a, b] = [b, a]
}

console.log(a)   // expect 7
console.log(b)   // expect 3

However, without a semicolon after the 7, or the enclosing braces, it is somehow interpreted as

var a = 3
var b = [b, a]

console.log(a)   // expect 3
console.log(b)   // expect [undefined, 3]