Avoiding Mutation
Work with copies instead of mutating data.
Why Avoid Mutation
Mutating shared data causes bugs that are hard to trace: a change in one place surprises code elsewhere. Functional style favors creating new values over modifying existing ones (immutability).
const original = [1, 2, 3];
const copy = [...original, 4];
console.log(original); // [1, 2, 3]
console.log(copy); // [1, 2, 3, 4]Spread to Copy Arrays
The spread operator ... creates a shallow copy of an array, letting you add elements without touching the source.
const nums = [1, 2, 3];
const more = [...nums, 4, 5];
console.log(more); // [1, 2, 3, 4, 5]
console.log(nums); // [1, 2, 3]