Mutating array methods

Arrays in JavaScript are mutable: you can change the original array directly, adding, removing, reordering or overwriting its elements. A mutating method changes the array you call it on, instead of giving you a new one. Every variable or component that holds a reference to that array sees the change.

const data = [1, 2, 3];
const sameData = data; // not a copy, same array

sameData.push(4);
data; // [1, 2, 3, 4]  (changed through the other variable)

const does not protect you here: it stops you from reassigning the variable, not from changing the array's contents.

Why it matters in React state

React decides whether to re-render by comparing the old and new state by reference (Object.is). If you mutate the state array and pass it back, the reference is the same, so React may skip the update, and any component or memo that kept the old array now sees changed data.

// wrong: same array, React may not re-render
todos.push(newTodo);
setTodos(todos);

// right: new array
setTodos((prev) => [...prev, newTodo]);
setTodos((prev) => prev.toSorted((a, b) => a.priority - b.priority));
setTodos((prev) => prev.toSpliced(index, 1));

Rule of thumb: mutating methods are fine on arrays you created yourself in the current function (local results, loops, buffers). For state, props and shared data, use a non-mutating alternative or copy first ([...arr].sort()).

The 9 mutating methods

MethodWhat it doesReturnsNon-mutating alternative
copyWithinCopies a range onto another position, same lengthThe same array[...arr].copyWithin(t, s, e) (no built-in)
fillOverwrites a range with one valueThe same array[...arr].fill(v, s, e) or arr.map(...)
popRemoves the last elementThe removed elementarr.slice(0, -1) (read it with arr.at(-1))
pushAdds elements at the endThe new length[...arr, x] or arr.concat(x)
reverseReverses the orderThe same arrayarr.toReversed()
shiftRemoves the first elementThe removed elementarr.slice(1) (read it with arr[0])
sortSorts (default: as strings)The same arrayarr.toSorted(fn)
spliceRemoves, inserts or replaces anywhereArray of removed elementsarr.toSpliced(s, n, ...items)
unshiftAdds elements at the startThe new length[x, ...arr]

toReversed, toSorted, toSpliced and with (ES2023) are available in Node.js 20+ and all current browsers.

Watch the return values

The return values are where most bugs come from:

  • push / unshift return the new length, so arr = arr.push(x) turns arr into a number.
  • pop / shift return the removed element.
  • splice returns an array of removed elements, not the updated array.
  • sort / reverse / fill / copyWithin return the same array, so const sorted = arr.sort() is not a copy: both names point to the sorted original.

Interview questions

1. Which array methods mutate the original array?

Answer

copyWithin, fill, pop, push, reverse, shift, sort, splice and unshift. Methods like map, filter, slice, concat, toSorted, toReversed, toSpliced and with return new arrays.

2. Why does setItems(items.sort()) sometimes not re-render in React?

Answer

sort mutates items and returns the same reference, so React sees the "new" state as identical to the old one. Use setItems(items.toSorted()) or setItems([...items].sort()).

3. Can you push into a const array?

Answer

Yes. const prevents reassigning the variable, not mutating the array it points to. Use Object.freeze(arr) if you need the contents to be read-only.