Skip to content

19 Rest parameter

Biswajit Sundara edited this page Aug 18, 2023 · 1 revision

A way to capture any remaining arguments in a function call and pass them to an array.

function sum(...numbers) {
  let result = 0;

  for (let number of numbers) {
    result += number;
  }

  return result;
}

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

Clone this wiki locally