About Lesson
Rest and Spread Syntax (…args)
1. Rest:
Allows a function to accept an indefinite number of arguments as an array.
Example:
JavaScript
function sum(...numbers) {
return numbers.reduce((total, num) => total + num);
// reduce() method is used to iterate through the array and accumulate a result by applying a function to each element.
}
console.log(sum(1, 2, 3)); // Output: 6
2. Spread:
Expands an array into individual elements
Example:
JavaScript
let arr = [1, 2, 3];
console.log(...arr); // Output: 1 2 3