Languages
JavaScript Array Methods Cheat Sheet
Array methods grouped by what they do, with a note on whether each one changes the original array.
Put it into practice with the matching tool.
Format JSON →Advertisement
Adding & removing
push(x)Add to the end — mutatespop()Remove the last item — mutatesunshift(x)Add to the start — mutatesshift()Remove the first item — mutatessplice(i, n, ...items)Remove/insert at an index — mutatesslice(start, end)Copy a portion — does not mutateconcat(arr)Join arrays into a new oneTransforming
map(fn)New array with each item transformedfilter(fn)New array with only items that pass the testreduce(fn, init)Collapse the array into a single valueflat(depth)Flatten nested arraysflatMap(fn)Map then flatten one leveljoin(sep)Combine into a stringSearching
find(fn)First item matching the test (or undefined)findIndex(fn)Index of the first match (or -1)findLast(fn)Last item matching the testindexOf(x)Index of a value (or -1)includes(x)True if the value existsTesting & iterating
forEach(fn)Run a function for each item (returns undefined)some(fn)True if at least one item passesevery(fn)True if all items passArray.isArray(x)Check whether a value is an arrayOrdering
sort((a,b) => a - b)Sort numerically — mutatessort()Sorts as strings by default (10 before 2)reverse()Reverse in place — mutatestoSorted() / toReversed()Newer non-mutating versionsCreating
Array.from(x)Build an array from an iterable or array-likeArray.from({length: 5}, (_, i) => i)Generate [0,1,2,3,4]Array.of(1, 2)Create an array from arguments[...new Set(arr)]Remove duplicates