1

Error Code

let a = 5
let b = 10
[a, b] = [b, a]; // ES6
console.log(a, b);

throw errro: Uncaught ReferenceError: b is not defined at :3:5

Running at Chrome Console, also throw error in Node.js

Valid Code

let a = 5;
let b = 10;
[a, b] = [b, a]; // ES6
console.log(a, b);

The difference is the semicolon in two let statements.

Ezio Shiki
  • 745
  • 6
  • 23
  • It is one of the case where `;` is necessary. `let b = 10 [a, b] = [b, a];` will treated as `let b = ((10)[(a, b)] = [b, a]);` – DecPK Sep 20 '21 at 04:16
  • And it's worth mentioning that `((10)[a, b] = [b, a])` is trying to assign properties to `10` after promoting it to a `Number` object. In strict mode this errors with " can't assign to property 10 on 10: not an object" because strict mode doesn't permit assigning properties to primitive value object wrappers. – traktor Sep 20 '21 at 05:06

0 Answers0