2

I just noticed that the following code, without immediately referencing one after let [one, two] = [1, 2], trying [one, two] = [two, one] would crash:

let [one, two, three] = [1, 2, 3]
[one, two] = [two, one] // CRASH! one is not defined
console.log(one, two)

However, simply adding a not-used one between the declaration and swapping suddenly allows the code, but incorrectly:

let [one, two, three] = [1, 2, 3]
one // inserted here
[one, two] = [two, one] // no longer crashes! but not actually swapping
console.log(one, two) // should be '2 1', but shows '1 2' instead

Whereas the below code gives the expected swapping effect

var a = 1;
var b = 3;

[a, b] = [b, a];
console.log(a); // 3
console.log(b); // 1

Can someone explain why such behavior exists? Thanks!

Kevin Qian
  • 2,532
  • 1
  • 16
  • 26

2 Answers2

6

Add a semicolon to the 1st line, because the interpreter assumes that lines 1 and 2 declarations happen together, and one (and two) is not defined yet:

let [one, two, three] = [1, 2, 3];
[one, two] = [two, one]
console.log(one, two)
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
  • 1
    Ooops, seem to be the case. Very interesting to see that losing semicolon is actually causing troubles. Thanks! – Kevin Qian Mar 15 '18 at 06:21
2

Cause its parsed like this:

 let [one, two, three] = [1, 2, 3][one, two] = [one, two]

To make it work, always suround destructuring assignments with parens:

 let [one, two, three] = [1, 2, 3];
 ([one, two] = [two, one]);
 console.log(one, two);

And never ever trust ASI, there are some cases like this were it goes wrong.

Rehan Haider
  • 893
  • 11
  • 26
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • Surrounding the assignment with parentheses doesn't help if the previous line doesn't have a semicolon. – Bergi Mar 15 '18 at 06:30
  • @bergi yes but its generally a good habit, especially when doing object destructuring – Jonas Wilms Mar 15 '18 at 06:42
  • Yes, for objects it's necessary when not in a variable declaration, but not for arrays. Here they seem superfluous to me. – Bergi Mar 15 '18 at 06:50