js rest arguments 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: rest parameters
function f(a, b) {
let normalArray = Array.prototype.slice.call(arguments)
let normalArray = [].slice.call(arguments)
let normalArray = Array.from(arguments)
let first = normalArray.shift()
let first = arguments.shift()
}
function f(...args) {
let normalArray = args
let first = normalArray.shift()
}
Example 3: spread and rest operator javascript
var myName = ["Marina" , "Magdy" , "Shafiq"] ;const [firstName , ...familyName] = myName ;console.log(firstName);
Example 4: 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);