Every array method youβll actually use, with clear examples.
π Transform
.map(fn)
[1, 2, 3].map(x => x * 2)β [2, 4, 6]
.flatMap(fn)
["hello world", "foo bar"].flatMap(s => s.split(" "))
β ["hello", "world", "foo", "bar"]
.flat(depth)
[[1, 2], [3, [4, 5]]].flat() [[1, 2], [3, [4, 5]]].flat(Infinity)β [1, 2, 3, [4, 5]]
β [1, 2, 3, 4, 5]
π Search & Filter
.filter(fn)
[1, 2, 3, 4, 5].filter(x => x > 3)β [4, 5]
.find(fn)
[{id: 1, name: "Alice"}, {id: 2, name: "Bob"}].find(u => u.id === 2)
β {id: 2, name: "Bob"}
.findIndex(fn)
["a", "b", "c"].findIndex(x => x === "b")β 1
.includes(value)
[1, 2, 3].includes(2)β true
.indexOf(value)
["a", "b", "c"].indexOf("b")
β 1
π Reduce & Test
.reduce(fn, initial)
[1, 2, 3, 4].reduce((sum, x) => sum + x, 0)β 10
// Count occurrences
["a","b","a","c","b","a"].reduce((acc, x) => {
acc[x] = (acc[x] || 0) + 1;
return acc;
}, {})
β {a: 3, b: 2, c: 1}
.every(fn) / .some(fn)
[2, 4, 6].every(x => x % 2 === 0) // all even? [1, 2, 3].some(x => x > 2) // any > 2?β true
β true
β Add & Remove
.push() / .pop()
const arr = [1, 2]; arr.push(3); // arr is now [1, 2, 3] arr.pop(); // returns 3, arr is [1, 2]
.unshift() / .shift()
const arr = [1, 2]; arr.unshift(0); // arr is now [0, 1, 2] arr.shift(); // returns 0, arr is [1, 2]
.splice(start, deleteCount, ...items)
const arr = ["a", "b", "c", "d"]; arr.splice(1, 2); // removes "b","c" β arr is ["a","d"] arr.splice(1, 0, "x","y"); // inserts at index 1 β ["a","x","y","d"]
.slice(start, end)
[0, 1, 2, 3, 4].slice(1, 3)β [1, 2]
.concat()
[1, 2].concat([3, 4]) // Or use spread: [...arr1, ...arr2]β [1, 2, 3, 4]
π Sort & Order
.sort(fn)
// Alphabetical (default) ["banana", "apple", "cherry"].sort()β [βappleβ, βbananaβ, βcherryβ]// Numeric (must provide comparator!) [10, 1, 21, 2].sort((a, b) => a - b)
// Descending [3, 1, 2].sort((a, b) => b - a)
β [1, 2, 10, 21]
β [3, 2, 1]
.reverse()
[1, 2, 3].reverse()β [3, 2, 1]
.toSorted() / .toReversed()
const arr = [3, 1, 2]; const sorted = arr.toSorted((a, b) => a - b); // arr is still [3, 1, 2], sorted is [1, 2, 3]
π§ Convert
.join(separator)
["hello", "world"].join(" ")
[2026, 3, 14].join("-")
β "hello world"β "2026-3-14"
Array.from()
Array.from("hello")
Array.from({length: 3}, (_, i) => i)
β ["h", "e", "l", "l", "o"]β [0, 1, 2]
[...new Set(arr)]
[...new Set([1, 2, 2, 3, 3, 3])]β [1, 2, 3]
Mutates vs Doesnβt Mutate
| Mutates (changes original) | Doesnβt mutate (returns new) |
|---|---|
| push, pop, shift, unshift | map, filter, slice, concat |
| splice, sort, reverse | flat, flatMap, toSorted |
| fill | toReversed, toSpliced |
When in doubt, use the non-mutating version. It prevents bugs.