Grokking Advanced JavaScript
0% completed
Rest Parameters
A restaurant bill can list two items or twenty, and the total is computed the same way each time: add up every price. A function that totals a bill has the same need. It must accept any number of arguments, and the person who writes the function cannot know in advance how many there will be.
A rest parameter handles this case. You write three dots before the last parameter's name, and JavaScript collects every remaining argument into a real array with that name.
function sum(...numbers) { // numbers is an array of every argument passed to sum }
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%