The Array prototype has many methods, and the difference between one that mutates in place and one that returns a fresh array is a frequent source of bugs. This reference lists each method with its signature, return type, mutability, and the ECMAScript version that added it.
How it works
Each method is classified by what it does to the array:
- Mutating —
push,pop,shift,unshift,splice,sort,reverse,fill,copyWithinchange the array in place. - Non-mutating —
map,filter,slice,concat,flat, and the rest return a new array or a single value, leaving the original untouched. - Immutable copies (ES2023) —
toSorted,toReversed,toSpliced, andwithare drop-in immutable versions of the mutating methods.
The reference tags each entry so you can see at a glance whether calling it is safe inside a pure function or a React render.
The classic confusion pairs
Several pairs of methods do similar things but differ critically on mutation or return value:
slice vs splice — slice(start, end) is non-mutating and returns a shallow copy of a portion of the array. splice(start, deleteCount, ...items) mutates the array in place, removing and optionally inserting elements, and returns the removed items. The similar names but opposite mutation behaviour cause bugs constantly.
sort vs toSorted — sort() mutates the original array and returns the same reference. toSorted() (ES2023) returns a new sorted copy. If you write const sorted = arr.sort(), you have also mutated arr — sorted and arr are the same array.
map vs forEach — Both iterate over every element, but map returns a new array of the results while forEach returns undefined. Use map when you need the transformed values; forEach when you only want side effects.
find vs filter — find returns the first matching element (or undefined); filter returns an array of all matching elements (possibly empty). Use find for a single lookup, filter for a collection.
ES2023 immutable copy methods
The toSorted, toReversed, toSpliced, and with methods were added in ES2023 specifically to provide immutable alternatives to sort, reverse, splice, and index assignment. They are drop-in replacements in functional code and React state handlers:
// Mutable — mutates the original
const arr = [3, 1, 2];
arr.sort(); // arr is now [1, 2, 3]
// Immutable (ES2023) — original unchanged
const sorted = arr.toSorted(); // new array [1, 2, 3]
// arr is still [3, 1, 2]
Tips
Inside React state updates and any functional pipeline, avoid the mutating
methods on data you do not own — sort a copy with [...arr].sort() or use
toSorted(). Remember reduce needs an initial value to be safe on possibly
empty arrays, and that map/filter/forEach skip holes in sparse arrays.
indexOf uses strict equality so it cannot find NaN; use includes, which
does, when NaN matters. flat and flatMap both handle nested arrays but
flatMap is a single pass that maps then flattens by one level, which is more
efficient than map followed by flat(1).