Consider the function provided in the example below.
In the example given above,
the function can only find the highest score from a specific number of scores.
In this case,
the function can only find the highest score from two scores.
Let's consider the following example,
where different function calls have different number of scores passed.
In the above example,
no matter how many extra arguments are passed,
the function gets executed
and
it only takes the value of the parameters name
, score1
, and score2
.
It is in such cases we need to use rest parameters.
The rest parameters method enables a function to have an unspecified number of arguments.
Let's modify the above example to use rest parameters.
In the above example,
the printHighestScore
always accepts the first argument as name
.
The rest of the arguments are grouped into an array scores
.
So for the function call printHighestScore("Oliver", 72, 65, 98)
,
the value of scores
is [72, 65, 98]
.
It is also important to note that the rest parameters can accept no parameters,
as shown in the example below.
In the example given above,
the value of scores
is []
.
That is why we get the value of Math.max(...scores)
as -Infinity
.
To prevent such unwanted edge cases,
let's put in a condition to exit the function
if the length of scores
is 0
.
The if
statement from the example above,
is called a guard clause.
It stops the execution of a function for certain a condition.
Do note that,
the rest parameters can only be used once in a function definition
and
should be used as the last parameter.