const [a, b = c, c] = [1, , 3]
The scenario depicted above can be viewed as
const a = [1, , 3][0];
const b = [1, , 3][1] || c; // Reference Error
const c = [1, , 3][2];
console.log(a, b, c);
In contrast, the following statement
const [a, b = c, c] = [1, , 3]
Can be envisioned as
const a = [1, , 3][0];
const b = [1, , 3][1] || a;
const c = [1, , 3][2];
console.log(a, b, c); //1 1 3
Thus, in the initial instance c
is referenced before being initialized leading to an error, while in the latter example, a
is already defined when accessed.
If you were to perform the same operation using var
, you would not encounter an error initially due to hoisting.
var [a, b = c, c] = [1, , 3]
console.log(a, b, c) //1 undefined 3