js spread operator parameter code example
Example 1: the rest operator javascript
function sum(...numbers) {
return numbers.reduce((accumulator, current) => {
return accumulator += current;
});
};
sum(1,2)
sum(1,2,3,4,5)
Example 2: array spread operator in javascript
let arr1 = ['A', 'B', 'C'];
let arr2 = ['X', 'Y', 'Z'];
let result = [...arr1, ...arr2];
console.log(result);
Example 3: js spread parameters
let list = ['a','b','c'];
let copy = [...list, 'd', 'e'];
function toDoList(...todos) {
document.write(
`<ul>${todos.map((todo) => `<li>${todo}</li>`).join("")}</ul>`
);
}
toDoList("wake up", "eat breakfast", ...list);