Approach: We want to implement a function that invokes another function and provides the arguments it received. We can get the result by using (…) the spread/rest operator.
Explaination: Let’s say, we create one function math that will be responsible for passing the arguments. We separate out our arguments for the math function. The first argument is reserved for the functionName and by giving “. . .params”, it will refer to other arguments as parameters for the functionName. We call the functionName and pass the rest of the arguments using “. . . params”.
In the following code, math takes many arguments. It reserves the first argument as the function name and the rest of the arguments as its parameters.
We can define different functions which will get their arguments from math function. We have defined function sum which takes all the arguments and make it as an array by taking them as (. . . args), and iterate through them to perform the task. Similarly, other functions like sub, mul, pow are implemented.
Output: The math function has successfully called the other functions and appended the required arguments.
20 -10 0
Example 2: The following example demonstrates a function that uses the first two arguments. It has used the first two arguments and our “args” is referring to the rest of the arguments leaving the first two arguments.
[ 3, 4, 5, 6 ]