June 2018
Beginner
288 pages
6h 31m
English
A rest parameter is defined using the ellipsis (...) to signify that that parameter is a placeholder for any number of arguments. The rest parameter directly addresses the issues with arguments. First, it stands for the rest of the parameters and so is highly visible in the parameter list. Second, the rest parameter is of Array type. Let’s convert the max() function from the previous example to use a rest parameter.
| | const max = function(...values) { |
| | console.log(values instanceof Array); |
| | |
| | let large = values[0]; |
| | |
| | for(let i = 0; i < values.length; i++) { |
| | if(values[i] > large) { |
| | large = values[i]; |
| | } |
| | } |
| | |
| | return large; |
| | }; |
| | |
| | console.log(max(2, 1, 7, 4)); |
The two versions ...
Read now
Unlock full access