Help with destructuring

Tell us what’s happening:

I’m a bit confused about how this destructuring problem works. I understand the const [a,b which assigns ‘a’ and ‘b’ to the 0 and 1 index. I also get that ...arr takes the rest of the array and makes another array with the indexes. What I don’t understand is why it has to equal list.

I kept thinking it would equal source since that is the array I’m destructuring. I don’t get what the arg list has to do with the problem.

Thank you in advance.

Your code so far


const source = [1,2,3,4,5,6,7,8,9,10];
function removeFirstTwo(list) {
"use strict";
// below
const [a,b,...arr] = list;  // <—-help here
// above
return arr;
}
const arr = removeFirstTwo(source);

Your browser information:

User Agent is: Mozilla/5.0 (iPhone; CPU iPhone OS 13_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 EdgiOS/45.3.19 Mobile/15E148 Safari/605.1.15.

Challenge: Use Destructuring Assignment with the Rest Parameter to Reassign Array Elements

Link to the challenge:

Because that is what they decided when they wrote the language. It could have been a different symbol but they chose the assignment operator. But, it’s not crazy. I mean:

const copyOfList = list;

makes sense.

But now instead of copyOfList, we are destructuring. And we are “assigning”, just through destructuring. True, they could have used some other symbol, but they didn’t. Don’t think about it too much.

Destructuring assingment makes the array passed into the function able to be unpacked. So, what you’re saying with

const [a,b,...arr] = list;

is 'Let the the array we are ‘unpacking’ be the array that was input, which inside our function is being called ‘list’ and was called ‘source’ outside of the function scope.

1 Like

Also, remember that = is not an equality operator, it is an assignment operator.

1 Like

Boom! I think that did it. I can see the process now that I realize I was thinking about it wrong.

Thank you!