I just noticed that the following code, without immediately referencing one
after let [one, two] = [1, 2]
, trying [one, two] = [two, one]
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)
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.