Rest parameters

 Rest parameters


Rest parameters allow you to pass an arbitrary number of arguments to a function as an array. Here's an example:


function sum(...numbers) {

  return numbers.reduce((total, number) => total + number, 0);

}


console.log(sum(1, 2, 3)); // Output: 6

console.log(sum(4, 5, 6, 7)); // Output: 22

In the code above, the sum function takes an arbitrary number of arguments using the rest parameter syntax (...numbers). The function then uses the reduce method to add up all the numbers and return the total.

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...