0

Look at the following pieces of code

let a = 1;
let b = 2;

[a,b] = [b,a]

console.log(a); // logs 2
console.log(b); // logs 1

now the same code with above, but without semicolons in the variable declarations - initializations:

let a = 1
let b = 2

[a,b] = [b,a]

console.log(a);
console.log(b); 

Now it throws the error: "Uncaught ReferenceError: Cannot access 'b' before initialization".

Why does this happen?

Dimitris
  • 3
  • 1
  • 4
  • It's because of the rules of [automatic semicolon insertion.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#automatic_semicolon_insertion) – Pointy Aug 07 '22 at 12:08
  • 1
    It reads `let b = 2[a,b]` – mplungjan Aug 07 '22 at 12:08

0 Answers0