Destructuring Assignment and Reassigning Array Elements

Tell us what’s happening:
This is concerning the problem:
“ES6: Use Destructuring Assignment with the Rest Parameter to Reassign Array Elements”

My code here works, and I’m not looking for a solution. But what I seek is understanding.

There is a declared const ‘source’ and the function removeFirstTwo(list)
List is the argument given. When we use this part of code

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

How does it know to use const source as the array. what if there were more than one array in the code?

Thank you for the clarification.

Your code so far


const source = [1,2,3,4,5,6,7,8,9,10];
function removeFirstTwo(list) {
"use strict";
// Only change code below this line 
const [a, b, ...arr] = list; // Change this line
// Only change code above this line
return arr;
}
const arr = removeFirstTwo(source);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36.

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

Link to the challenge:


If we make source an array containing three subarrays, the function removes the first two subarrays.

removeFirstTwo will remove the first two items (index 0 and 1) of the main array.

2 Likes

Hello @wardthomas17
I got the same kind of confusion on anther chapter yesterday, the thing is that we must distinguish the setup part of a function and the use part (execution) of this one, as you can see in the end of the code :

this part is where we are calling the function to be executed and we gave as an argument source whiche is the name of the declared array, so JavaScript will replace list that we used on the setup part and puts source every list keyword while executing the function.

1 Like

A little reexamination and it all works. It seems like making the function work fulfills the correctness of the last line of the code. Thank you for steering me to what i missed.

1 Like

I was more so looking to understand how the code knew to use “source” as the array, and I see now that it was declared on line 9 as the argument of the function to replace “list”.

2 Likes