W3docs

JavaScript Array Methods

Learn the essential JavaScript array methods with runnable examples: push, pop, map, filter, reduce, slice, splice, and find.

Introduction

Arrays are one of the most-used data structures in JavaScript, and most of their power comes from their built-in methods — functions you call on an array to add, remove, search, transform, or reorder its elements. This guide covers the methods you reach for every day, explains the all-important difference between methods that mutate the original array and those that return a new array, and shows each one with a runnable example.

If you are new to arrays themselves, start with the JavaScript Array chapter, then come back here.

Understanding JavaScript Arrays

An array stores an ordered list of values in a single variable. The values can be of any of the JavaScript data types — numbers, strings, objects, even other arrays — and you do not have to decide the size in advance.

Creating an Array

The simplest way to create an array is with square-bracket literal syntax:

let fruits = ["Apple", "Banana", "Cherry"];

This creates an array named fruits with three string elements.

Accessing Array Elements

Elements are accessed by their index, a zero-based position counting from the start of the array:

javascript— editable

The length property tells you how many elements there are, and at() accepts negative indexes so at(-1) always gives you the last element.

Mutating vs. Non-Mutating Methods

Before looking at individual methods, learn this distinction — it explains most array bugs:

  • Mutating methods change the array in place. push, pop, shift, unshift, splice, sort, reverse, and fill all modify the array you call them on.
  • Non-mutating methods leave the original untouched and return a new array or value. map, filter, slice, concat, reduce, find, and the newer toSorted/toSpliced/toReversed fall into this group.

When you share an array across functions or store it in state (for example in a React component), prefer non-mutating methods so you do not accidentally change data somebody else is relying on.

Core Array Methods

MethodMutates?Description
push()YesAdds one or more elements to the end; returns the new length.
pop()YesRemoves the last element and returns it.
shift()YesRemoves the first element and returns it.
unshift()YesAdds elements to the beginning; returns the new length.
splice()YesRemoves, replaces, and/or inserts elements at any position.
sort()YesSorts the elements (alphabetically by default).
reverse()YesReverses the order of the elements.
fill()YesOverwrites a range of elements with a static value.
slice()NoReturns a shallow copy of a portion of the array.
concat()NoMerges arrays and returns a new array.
map()NoReturns a new array of the results of a function on each element.
filter()NoReturns a new array of elements that pass a test.
reduce()NoReduces the array to a single value.
forEach()NoRuns a function for each element (returns undefined).
find()NoReturns the first element that satisfies a test.
findIndex()NoReturns the index of the first element that satisfies a test.
indexOf()NoReturns the first index of a value, or -1.
includes()NoReturns true/false for whether a value exists.
some()Notrue if at least one element passes a test.
every()Notrue if all elements pass a test.
toSorted()NoReturns a sorted copy (the array is unchanged).
toSpliced()NoReturns a spliced copy (the array is unchanged).

Adding and Removing Elements

push()

push() appends one or more elements to the end of the array and returns the new length.

javascript— editable

unshift()

unshift() inserts elements at the beginning of the array.

javascript— editable

pop()

pop() removes the last element and returns it. Together with push(), it lets an array act as a stack (last in, first out).

javascript— editable

shift()

shift() removes the first element and returns it. Note that shift and unshift are slower than push/pop because every remaining element has to be re-indexed.

javascript— editable

Finding Elements

indexOf()

indexOf() returns the first index of a value, or -1 if the value is not found. It uses strict equality, so it cannot find objects by their contents.

javascript— editable

includes()

includes() answers a simpler yes/no question: is this value in the array? It is clearer than indexOf(x) !== -1 and, unlike indexOf, it can find NaN.

javascript— editable

find() and findIndex()

When you need to locate an element by a condition rather than an exact value, use find() (returns the element) or findIndex() (returns its index). Both stop at the first match.

javascript— editable

some() and every()

some() returns true if at least one element passes the test; every() returns true only if all of them do.

javascript— editable

Iterating and Transforming

These methods take a callback function and are the heart of modern, declarative JavaScript. They pair naturally with the patterns in the JavaScript Loops chapter.

forEach()

forEach() runs a function once per element. It always returns undefined, so use it for side effects (like logging), not for building a new value.

javascript— editable

map()

map() creates a new array by transforming each element. The new array always has the same length as the original.

javascript— editable

filter()

filter() creates a new array containing only the elements for which the callback returns a truthy value.

javascript— editable

reduce()

reduce() boils an array down to a single value by repeatedly applying a function. The callback receives an accumulator and the current element; the second argument to reduce is the starting value of the accumulator.

javascript— editable

You can chain these transformers: map first, then filter, then reduce, each step producing the input for the next.

Reordering and Slicing

sort()

sort() sorts in place. By default it compares elements as strings, which gives surprising results for numbers — 10 sorts before 2 because "10" comes before "2" alphabetically. Always pass a compare function when sorting numbers.

javascript— editable

reverse()

reverse() flips the order of the elements in place.

javascript— editable

slice()

slice(start, end) returns a shallow copy of a portion of the array. The end index is not included, and the original is left untouched — making slice() a handy way to copy an array.

javascript— editable

splice()

splice(start, deleteCount, ...items) is the all-purpose in-place editor: it can remove, replace, and insert. Here it inserts "Grape" at index 2 without deleting anything (deleteCount is 0).

javascript— editable

concat()

concat() merges arrays into a new array without changing the originals. The spread syntax ([...a, ...b]) does the same thing — see Rest parameters and spread syntax.

javascript— editable

fill()

fill(value, start, end) overwrites a range of elements with a static value, in place.

javascript— editable

Immutable (Copying) Methods

Introduced in ES2023, these methods do exactly what sort, splice, and reverse do — but they return a new array and leave the original alone. They are ideal for state you must not mutate.

toSorted()

javascript— editable

toSpliced()

javascript— editable

Conclusion

Mastering JavaScript array methods is one of the highest-leverage skills in the language: map, filter, and reduce replace most manual loops, while knowing which methods mutate versus copy prevents a whole class of bugs. Keep practicing with the runnable examples above, and explore related topics like the JavaScript Array basics and spread syntax.

Practice

Practice
Which of the following array methods can be used in JavaScript to add and remove elements from an array?
Which of the following array methods can be used in JavaScript to add and remove elements from an array?
Practice
What does the map() method return?
What does the map() method return?
Practice
Why should you pass a compare function to sort() when sorting numbers?
Why should you pass a compare function to sort() when sorting numbers?
Was this page helpful?