sort and splice
These methods change the array in place — unless you copy first.
sort and splice mutate. Prefer
[...arr].sort(...) or arr.slice() when you must
keep the original. Modern engines also have toSorted /
toReversed; this lab teaches the copy-first pattern.
Working array A (may mutate)
Working array B / copy result
Important JavaScript
const scores = [30, 10, 20];
scores.sort((a, b) => a - b);
// scores is now [10, 20, 30] — mutated
const safe = [...scores].sort((a, b) => b - a);
// new sorted array; original left alone if you copied first
const names = ["Ava", "Ben", "Cara"];
names.splice(1, 1, "Blair");
// removed 1 item at index 1, inserted "Blair"
const next = names.slice();
next.splice(0, 1);
// mutate only the copy