JavaScript Array Methods

Interactive reference for all JS array methods with live editable code examples. Edit, run, and learn.

15.9Kuses
8.1/10(106)

Adding & Removing

Methods that add or remove elements

push()Adds elements to the end
MutatesReturns: New length
arr.push(...items)
pop()Removes the last element
MutatesReturns: Removed element
arr.pop()
unshift()Adds elements to the beginning
MutatesReturns: New length
arr.unshift(...items)
shift()Removes the first element
MutatesReturns: Removed element
arr.shift()
splice()Adds/removes elements at any position
MutatesReturns: Array of removed
arr.splice(start, deleteCount, ...items)

Transformation

Methods that create new transformed arrays

map()Creates a new array by transforming each element
Non-mutatingReturns: New array
arr.map(callback(item, index, arr))
filter()Creates a new array with elements that pass a test
Non-mutatingReturns: New array
arr.filter(callback(item, index, arr))
reduce()Reduces array to single value by accumulating
Non-mutatingReturns: Accumulated value
arr.reduce(callback(acc, item, index, arr), initialValue)
flat()Flattens nested arrays to specified depth
Non-mutatingReturns: New array
arr.flat(depth)
flatMap()Maps then flattens one level deep
Non-mutatingReturns: New array
arr.flatMap(callback)
slice()Returns a shallow copy of a portion
Non-mutatingReturns: New array
arr.slice(start, end)
concat()Merges arrays together
Non-mutatingReturns: New array
arr.concat(...arrays)

Search & Test

Methods for finding elements and testing conditions

find()Returns first element that passes a test
Non-mutatingReturns: Element or undefined
arr.find(callback(item, index, arr))
findIndex()Returns index of first matching element
Non-mutatingReturns: Index or -1
arr.findIndex(callback)
indexOf()Returns first index of an element
Non-mutatingReturns: Index or -1
arr.indexOf(item, fromIndex)
includes()Checks if array contains an element
Non-mutatingReturns: Boolean
arr.includes(item, fromIndex)
every()Tests if ALL elements pass
Non-mutatingReturns: Boolean
arr.every(callback)
some()Tests if ANY element passes
Non-mutatingReturns: Boolean
arr.some(callback)

Sorting & Ordering

Methods that change element order

sort()Sorts elements in place
MutatesReturns: Sorted array
arr.sort(compareFn)
reverse()Reverses order of elements in place
MutatesReturns: Reversed array
arr.reverse()
toSorted()Returns a new sorted array (non-mutating)
Non-mutatingReturns: New sorted array
arr.toSorted(compareFn)
toReversed()Returns a new reversed array (non-mutating)
Non-mutatingReturns: New reversed array
arr.toReversed()

Iteration

Methods for looping over elements

forEach()Calls a function for each element (no return value)
Non-mutatingReturns: undefined
arr.forEach(callback(item, index, arr))
entries()Returns iterator of [index, value] pairs
Non-mutatingReturns: Iterator
arr.entries()
keys()Returns iterator of indices
Non-mutatingReturns: Iterator
arr.keys()
values()Returns iterator of values
Non-mutatingReturns: Iterator
arr.values()

Conversion

Methods that convert arrays to other types

join()Joins elements into a string
Non-mutatingReturns: String
arr.join(separator)
toString()Converts array to comma-separated string
Non-mutatingReturns: String
arr.toString()
Array.from()Creates array from iterable or array-like
Non-mutatingReturns: New array
Array.from(iterable, mapFn)
Array.isArray()Checks if value is an array
Non-mutatingReturns: Boolean
Array.isArray(value)

Other

Additional useful methods

fill()Fills elements with a static value
MutatesReturns: Modified array
arr.fill(value, start, end)
copyWithin()Copies part of array to another location in same array
MutatesReturns: Modified array
arr.copyWithin(target, start, end)
with()Returns new array with element replaced at index
Non-mutatingReturns: New array
arr.with(index, value)
at()Returns element at index (supports negative)
Non-mutatingReturns: Element
arr.at(index)
⚡ Pro OptionsSponsored

Some links on this page are affiliate links. If you click and make a purchase, we may earn a commission at no extra cost to you.

Recommended Products

Ad

Affiliate Disclosure: As an Amazon Associate, ToolBird earns from qualifying purchases. Links above are affiliate links — if you buy through them, we may earn a small commission at no extra cost to you.

Disclaimer: This tool is provided as-is for informational and educational purposes only.

ToolBird Assistant

Find the right tool instantly

Hey! I'm ToolBird Assistant. Tell me what you need and I'll find the right tool for you.