☰ See All Chapters |
JavaScript ES6 Rest Parameter - Varag Parameters
JavaScript ES6 functions can also accept a variable number of arguments. Only one varag is allowed and varag should be the last parameter. Varag parameter requires preceding name with ..., as shown in the following code:
let addNums = (message, ...nums) => { console.log(message); let sum = 0; for (let i of nums) { sum = sum + i; } return sum; };
addNums("Hi"); addNums("Hi", 10); addNums("Hi", 10, 20); addNums("Hi", 10, 20, 30); |
|
The rest parameter with the following syntax will cause an error because varag should be the last parameter in parameter list.
function show(a,...rest, b) { // error }; |
All Chapters