Using the rest operator in function's parameter section

I’ve watched Beau’s videos and browsed MDN and w3 and I’m still not sure I understand the use of rest at least in the context of this particular lesson. Below is my attempt. What I think I have done is open the array of sum to args through modification of the function in return. I don’t think that I need to use it again further down, but I’m not really sure what to do and the little that I think I know about rest and spread assumes that I’m opening up the array up to a number of parameters through rest.

Help, por favor.


const sum = (function() {
  "use strict";
  return function sum(...args) {
    const args = [ x, y, z ];
    return args.reduce((a, b) => a + b, ...args);
  };
})();
console.log(sum(1, 2, 3)); // 6

I think you kind of have it but may be overthinking rest parameter. Rest parameter just packs up an undetermined number of parameters into an array that you can easily access in your function.


function myFunc(...args){
  console.log(args); // all param in array
}

myFunc(1,2,3);  
//[ 1, 2, 3 ]

myFunc("cow", "duck", "chicken", "pig");  
//[ 'cow', 'duck', 'chicken', 'pig' ]


////////////////////////
// and again with a named parameter
function myFunc2(first, ...theRest){
  console.log(first);
  console.log(theRest); // all but 1st param in array
}

myFunc2(1,2,3);
// 1
// [ 2, 3 ]

myFunc2("cow", "duck", "chicken", "pig"); 
// cow
// [ 'duck', 'chicken', 'pig' ]