1

I was looking for info on JavaScript destructuring and found the video "Destructuring Assignment" as part of a video series from Packt Publication. At the very beginning of the video, I saw the following code:

var [a, b] = [1,2,3];
a === 1;
b === 3;

The presenter then explains why variable b is 3 and not 2, which didn't seem correct to me, but I thought maybe I'm wrong.

So I did a Code Pen with the following code:

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

As I expected, the variable b is 2.

Is there something I'm missing and not understanding?

Below is a screenshot of the video in questions.

enter image description here

Mike Barlow - BarDev
  • 11,087
  • 17
  • 62
  • 83

1 Answers1

4

Yes, the video is wrong, these below are the only ways to get the 3 in this array (using two variables names):

const [a, , b] = [1, 2, 3]; // b is 3
const [a, ...b] = [1, 2, 3]; // b is [2, 3], so b[1] is 3

Also, see: Destructuring to get the last element of an array in es6

Edmundo Santos
  • 8,006
  • 3
  • 28
  • 38