ES6 - Destructuring via rest elements

Tell us what’s happening:
Describe your issue in detail here.
I understand the answer as to why it is what it is but I don’t understand the code completely… Can someone explain to me in simple words the following questions i have?

  1. How are we able to pass in the list parameter but also define it within the function?
  2. What does the following line of code do in the context of this problem and is it also the reason why we are able to pass in the source into the removeFirstTwo function?
    const sourceWithoutFirstTwo = removeFirstTwo(source);
  3. If I have to summarize what confuses me its this. I know what the following code does
    function removeFirstTwo(list) {
    // Only change code below this line
    const [a,b,…shorterList] = list; // Change this line
    // Only change code above this line
    return shorterList;

But I DO NOT UNDERSTAND WHAT THE CODE BELOW DOES
const source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const sourceWithoutFirstTwo = removeFirstTwo(source);
}

Your code so far

function removeFirstTwo(list) {
  // Only change code below this line
  const [a,b,...shorterList] = list; // Change this line
  // Only change code above this line
  return shorterList;
}

const source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const sourceWithoutFirstTwo = removeFirstTwo(source);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36

Challenge: ES6 - Destructuring via rest elements

Link to the challenge:

list is not being defined within the function. It is being used to set the values of other variables inside the function, such as a, b, and shorterList.

It calls the function removeFirstTwo, passing in the source variable as the argument, and then sets the variable sourceWithoutFirstTwo to the value returned by the function.

This is just creating an array of numbers and assigning it to the variable source so that you can pass it into the function removeFirstTwo.

I just wanted to take this chance to say thank you for your explanations!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.