js triple dot operator code example
Example 1: javascript ... operator three dots
function sum(x, y, z) {
return x + y + z;
}
const numbers = [1, 2, 3];
console.log(sum(...numbers));
console.log(sum.apply(null, numbers));
function sum(...numbers) {
let sum = 0;
for (const number of numbers)
sum += number;
return sum;
}
console.log(sum(1, 2, 3, 4, 5));
console.log(sum(4, 5, ...numbers));
Example 2: three dots in javascript
let team = {
lead: "Bob",
num2: "Rob",
num3: "Dog",
}
{lead: "Bob", num2: "Rob", num3: "Dog"}
let students = {
num1: "Andy",
num4: "Ben",
...team
}
{num1: "Andy", num4: "Ben", lead: "Bob", num2: "Rob", num3: "Dog"}
Example 3: 3 dots to get all object properties in JS
this.setState(prevState => {
return {foo: {...prevState.foo, a: "updated"}};
});