JavaScript Array Methods

All Array prototype methods with signature, return type and mutability flag

Searchable JavaScript Array method reference. Each entry lists the call signature, return type, whether the method mutates the array in place or returns a new one, and the ECMAScript version that introduced it. It runs free in your browser on Gera Tools, with nothing uploaded.

Last updated Source: Gera Tools

Which array methods mutate the original array?

The mutating methods are push, pop, shift, unshift, splice, sort, reverse, fill, and copyWithin. They change the array in place and usually return a length or the array itself rather than a fresh copy. Everything else returns a new array or a value.

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:

  • Mutatingpush, pop, shift, unshift, splice, sort, reverse, fill, copyWithin change the array in place.
  • Non-mutatingmap, 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, and with are 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 spliceslice(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 toSortedsort() 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 arrsorted 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 filterfind 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).