Outline

Rest parameters in functions take all the arguments passed into a function, starting from a given point, and put them into one single parameter inside the function as an array. Their index in the array depends on how they were passed to the function. The array can then be used however you'd like.

Here's an example of what this looks like:

function addNumbers(name, ...numbers) {
  let sum = 0;
  for (const num of numbers) {
    sum += num;
  }
  return 'Total of ' + name + ': ' + sum;
}

const totalPrice = addNumbers('Price', 1, 2, 3, 4, 5);

In this example, the numbers 1, 2, 3, 4, and 5 are all added into the numbers parameter as the rest parameter.

The only thing to remember is that once you've declared a rest parameter, you can't declare any other parameters, including other rest parameters, after it. You'll get an error in your program if you try to do that.

You can learn more about rest parameters in this blog post.

 

I finished! On to the next chapter