JavaScript arrays come with a rich set of methods for looping, transforming, and searching. This cheat sheet groups the methods you use most and notes which ones change the original array.
Iterate and transform
| Method | What it does |
|---|---|
forEach(fn) |
Run a function for each item; returns nothing |
map(fn) |
New array with each item transformed |
filter(fn) |
New array with the items that pass a test |
reduce(fn, init) |
Boil the array down to a single value |
reduceRight(fn, init) |
Like reduce, but from right to left |
flatMap(fn) |
Map each item, then flatten one level |
Test and search
| Method | What it does |
|---|---|
find(fn) |
First item that passes a test, or undefined |
findIndex(fn) |
Index of the first match, or -1 |
findLast(fn) |
First match searching from the end |
some(fn) |
True if at least one item passes |
every(fn) |
True if every item passes |
includes(v) |
True if the value is present |
indexOf(v) |
Position of a value, or -1 |
at(i) |
Item at an index; supports negatives, e.g. at(-1) |
Add, remove, copy
| Method | What it does |
|---|---|
push(v) / pop() |
Add or remove at the end (mutates) |
unshift(v) / shift() |
Add or remove at the start (mutates) |
splice(i, n, ...) |
Remove or insert at an index (mutates) |
slice(start, end) |
Copy a section into a new array |
concat(arr) |
Join arrays into a new one |
flat(depth) |
Flatten nested arrays by a depth |
fill(v) |
Fill the array with one value (mutates) |
Order and build
| Method | What it does |
|---|---|
sort(fn) |
Sort in place; pass a comparator for numbers (mutates) |
reverse() |
Reverse the order in place (mutates) |
join(sep) |
Join items into a string |
toSorted() / toReversed() |
Newer versions that return a new array |
Array.from(x) |
Build an array from an iterable or array-like |
Array.of(...) |
Build an array from the arguments |
Array.isArray(x) |
Check whether a value is an array |
keys() / values() / entries() |
Iterators over indexes, values, and pairs |
Frequently Asked Questions
Which array methods change the original array?
push, pop, shift, unshift, splice, sort, reverse, and fill mutate in place. map, filter, slice, and concat return a new array and leave the original alone.
What is the difference between map and forEach?
map returns a new array of transformed values. forEach runs a function for side effects and returns undefined, so it is for doing rather than building.
How does reduce work?
reduce walks the array carrying an accumulator. Your function returns the next accumulator each step, and the final value is the result, useful for sums, counts, and grouping.
How do I sort numbers correctly?
sort compares as text by default, so 10 comes before 2. Pass a comparator like (a, b) => a – b to sort numbers in true numeric order.
Working with data? Try our free JSON Formatter and other developer tools.