Skip to main content

JavaScript Array Methods

Web

JavaScript Array Methods

# JavaScript Array Methods

The methods you will use constantly.

Transforming

MethodReturns
map(fn)New array of transformed items
filter(fn)New array of items that pass a test
reduce(fn, init)A single accumulated value
flat()Flattened nested arrays
flatMap(fn)map then flat one level

Searching

MethodReturns
find(fn)First matching item (or undefined)
findIndex(fn)Index of first match (or -1)
includes(x)true if value exists
indexOf(x)Index of value (or -1)
some(fn) / every(fn)Boolean: any / all pass

Adding & removing

MethodEffect
push(x) / pop()Add / remove at end
unshift(x) / shift()Add / remove at start
slice(a, b)Copy a portion (non-mutating)
splice(i, n)Remove/insert in place (mutating)

Example

js
12345
const nums = [1, 2, 3, 4];
const doubledEvens = nums
  .filter(n => n % 2 === 0)
  .map(n => n * 2);
// [4, 8]
← All cheat sheets