Sub topic 4
Which Methods Change Your Array?
This chapter assumes you can declare variables with let and const, write arrow functions, and run code in Node.js or a browser console. You should also know how to create an array and read elements by index.
Every array method does one of two things. A mutating method changes the array you call it on. A non-mutating method leaves that array alone and returns something new. If you mix the two up, you get bugs that are hard to trace, because a change made in one place shows up somewhere else.
Arrays Are Shared by Reference
When you assign an array to a second variable, you do not get a copy. Both variables point to the same array in memory. A mutation through one variable is visible through the other.
const scores = [30, 10, 20];
const alias = scores;
alias.push(40);
console.log(scores); // [30, 10, 20, 40]
console.log(alias === scores); // trueconst does not protect you here. It stops you from reassigning the variable. It does not stop you from changing the contents of the array.
The Two Groups
| Mutating (changes the original) | Non-mutating (returns a new value) |
|---|---|
| `push`, `pop`, `shift`, `unshift` | `slice`, `concat` |
| `splice` | `toSpliced` |
| `sort`, `reverse` | `toSorted`, `toReversed` |
| `fill`, `copyWithin` | `with` |
| `map`, `filter`, `reduce`, `find`, `includes`, `join` |
The ES2023 methods toSorted, toReversed, toSpliced, and with were added as copying versions of sort, reverse, splice, and index assignment.
Return Values Are a Common Trap
Mutating methods often return something you might not expect:
pushandunshiftreturn the new length, not the array.popandshiftreturn the removed element.splicereturns an array of the removed elements.sortandreversereturn the same array they changed, not a copy.
The last one catches many developers. Assigning the result of sort to a new variable does not protect the original:
const original = [3, 1, 2];
const sorted = original.sort((a, b) => a - b);
console.log(sorted); // [1, 2, 3]
console.log(original); // [1, 2, 3] (also changed)
console.log(sorted === original); // true
const data = [3, 1, 2];
const safe = data.toSorted((a, b) => a - b);
console.log(safe); // [1, 2, 3]
console.log(data); // [3, 1, 2] (unchanged)A similar mistake is const list = items.push(x), which stores a number in list.
Choosing Between Them
Use a non-mutating method when other code holds the same array. Examples include function arguments, component state in UI frameworks, and shared configuration. A function that sorts its argument in place changes data belonging to the caller.
Use a mutating method when you own the array and nobody else holds it. A local array you build inside a loop is one example. Mutating in place skips the copy, which can matter for large arrays.
Later chapters cover the adding, removing, and copying methods in detail.
Exercise
After running const a = [5, 2, 8]; const b = a.reverse(); const c = a.length; const d = a.push(1); what are the values of a, b === a, and d?
Show answer
a is [8, 2, 5, 1] because reverse and push both changed it in place. b === a is true because reverse returns the same array. d is 4 because push returns the new length.
Exercise
A function top3(list) returns list.sort((x, y) => y - x).slice(0, 3). Why might callers complain, and how would you fix it?
Show answer
sort changes the caller's array in place, so the caller's data ends up reordered. Use list.toSorted((x, y) => y - x).slice(0, 3), or [...list].sort(...) in environments without ES2023, so that the function sorts a copy.