SpinSpire logo

Mutations, Rest Parameter, and Spread Operator

Mutations

const x = [1, 2, 3];
x[1] = 30;
console.log(x); //will display [1, 30, 3] cause we mutated index [1], changing 2 to 30.

Rest Parameter

const sum = (...args) => {
return args.reduce((x, y) => x + y, 0);
// 0 is just the starter value
// reduce creates one value from multiple args in this situation.
}
console.log(sum(5, 10, 20)); // 35

Spread Operator

const food = ["Pizza", "Sushi", "Fruit"];
const moreFood = ["Wings", "Fish", ...food];
console.log(moreFood) =
// Wings, Fish, Pizza, Sushi, Fruit