js Spread parameters code example
Example 1: spread operator in javascript
let numList = [1,2,3];
let numListClone = [...numList];
let animal = {
name: 'dog',
color: 'brown',
age: 7
};
let { age, ...otherProperties } = animal;
function sum(x, y, ...rest) {}
let numLists = [...numList1, ...numList2];
let animalWithBreed = {
...animal,
breed: '',
}
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: spread operator javascript
obj = {first_name : "Marty",
lovers: ["Jennifer Parker","Baines McFly"]
};
let objClone = { ...obj };
console.log(objClone)
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);