TetSharing

JavaScript array methods cheat sheet

The first group changes the array in place; every other method returns a new value and leaves the original alone. Results use the same compact format as our runner: strings in double quotes, arrays as [a, b]. Lessons: arrays and map, filter, reduce.

Change the array in placeTransform into a new arraySearch and testCreate, convert, iterate

Change the array in place

SyntaxWhat it doesExample → result
arr.push(...items)Add to the end; returns the new lengthconst a = [1, 2]; a.push(3); a
→ [1, 2, 3]
arr.pop()Remove and return the last item[1, 2, 3].pop()
→ 3
arr.unshift(...items)Add to the frontconst a = [2]; a.unshift(1); a
→ [1, 2]
arr.shift()Remove and return the first item[1, 2, 3].shift()
→ 1
arr.splice(start, n, ...items)Remove n items at start and insert itemsconst a = [1, 2, 3]; a.splice(1, 1, "x"); a
→ [1, "x", 3]
arr.reverse()Reverse in place[1, 2, 3].reverse()
→ [3, 2, 1]
arr.sort(compare)Sort in place; pass (a, b) => a - b for numbers[10, 1, 5].sort((a, b) => a - b)
→ [1, 5, 10]
arr.sort()Default sort compares as strings[10, 1, 5].sort()
→ [1, 10, 5]
arr.fill(value)Fill every slot with a valuenew Array(3).fill(0)
→ [0, 0, 0]

Transform into a new array

SyntaxWhat it doesExample → result
arr.map(fn)New array of fn(item) for every item[1, 2, 3].map((n) => n * 2)
→ [2, 4, 6]
arr.filter(fn)Items for which fn returns truthy[1, 2, 3, 4].filter((n) => n % 2 === 0)
→ [2, 4]
arr.reduce(fn, init)Fold all items into one value[1, 2, 3].reduce((s, n) => s + n, 0)
→ 6
arr.flat(depth)Flatten nested arrays (default depth 1)[1, [2, [3]]].flat()
→ [1, 2, [3]]
arr.flatMap(fn)map, then flatten one level["a b", "c"].flatMap((s) => s.split(" "))
→ ["a", "b", "c"]
arr.slice(start, end)Copy of a section[1, 2, 3, 4].slice(1, 3)
→ [2, 3]
arr.concat(...arrays)Join arrays into a new one[1].concat([2, 3])
→ [1, 2, 3]
[...arr]Shallow copy or merge with spread[...[1, 2], 3]
→ [1, 2, 3]
arr.toSorted()Sorted copy (ES2023)[3, 1, 2].toSorted()
→ [1, 2, 3]
arr.toReversed()Reversed copy (ES2023)[1, 2, 3].toReversed()
→ [3, 2, 1]
arr.with(i, value)Copy with one item replaced (ES2023)[1, 2, 3].with(1, 9)
→ [1, 9, 3]
arr.join(sep)String of all items with sep between["a", "b"].join("-")
→ "a-b"
SyntaxWhat it doesExample → result
arr.indexOf(x)First index of x, or -1["a", "b"].indexOf("b")
→ 1
arr.lastIndexOf(x)Last index of x, or -1[1, 2, 1].lastIndexOf(1)
→ 2
arr.includes(x)true if present (finds NaN too)[1, 2, NaN].includes(NaN)
→ true
arr.find(fn)First matching item, or undefined[5, 12, 8].find((n) => n > 6)
→ 12
arr.findIndex(fn)Index of the first match, or -1[5, 12, 8].findIndex((n) => n > 6)
→ 1
arr.findLast(fn)Last matching item (ES2023)[5, 12, 8].findLast((n) => n > 6)
→ 8
arr.some(fn)true if any item matches[1, 2].some((n) => n > 1)
→ true
arr.every(fn)true if all items match[1, 2].every((n) => n > 1)
→ false
arr.at(i)Item at i; negative counts from the end[1, 2, 3].at(-1)
→ 3

Create, convert, iterate

SyntaxWhat it doesExample → result
Array.from(iterable)Array from a string, Set, Map or array-likeArray.from("abc")
→ ["a", "b", "c"]
Array.from({ length }, fn)Generate items by indexArray.from({ length: 3 }, (_, i) => i * i)
→ [0, 1, 4]
Array.of(...items)Array of the argumentsArray.of(7)
→ [7]
Array.isArray(x)Reliable array testArray.isArray([])
→ true
Object.entries(obj)Array of [key, value] pairsObject.entries({ a: 1 })
→ [["a", 1]]
Object.fromEntries(pairs)Object from [key, value] pairsObject.fromEntries([["a", 1]])
→ {a: 1}
arr.lengthNumber of items[1, 2, 3].length
→ 3
arr.forEach(fn)Run fn for each item; returns undefinedlet s = 0; [1, 2].forEach((n) => { s += n; }); s
→ 3
arr.entries()Iterator of [index, item] pairs[...["a", "b"].entries()]
→ [[0, "a"], [1, "b"]]

Official docs

Compiled by Ethan Cole (pen name) with AI-assisted drafting; every example in the table is executed by our test suite. Related lessons: arrays, array methods.