JavaScript Array Methods: A Practical Guide
3 min read · Published
Table of contents
Arrays are the workhorse data structure of JavaScript. Knowing the built-in methods well lets you replace most hand-written loops with short, readable, declarative code. This guide groups them by purpose and flags which ones mutate the original array.
Quick reference
| Goal | Method | Returns | Mutates? |
|---|---|---|---|
| Transform each item | map |
new array | no |
| Keep some items | filter |
new array | no |
| Fold to one value | reduce |
any value | no |
| First match | find, findIndex |
item / index | no |
| Any / all match | some, every |
boolean | no |
| Contains value | includes, indexOf |
boolean / index | no |
| Add / remove at end | push, pop |
length / item | yes |
| Add / remove at start | unshift, shift |
length / item | yes |
| Insert / remove anywhere | splice |
removed items | yes |
| Copy a part | slice |
new array | no |
| Sort | sort, toSorted |
array | sort yes, toSorted no |
| Flatten | flat, flatMap |
new array | no |
Transforming with map
const users = [{ name: 'Ada', age: 36 }, { name: 'Linus', age: 54 }];
const names = users.map((u) => u.name); // ['Ada', 'Linus']
map always returns an array of the same length. If you do not use the returned array, use forEach instead.
Filtering
const adults = users.filter((u) => u.age >= 40); // [{ name: 'Linus', age: 54 }]
Chain filter and map freely: users.filter(...).map(...).
Reducing
const total = [10, 20, 30].reduce((sum, n) => sum + n, 0); // 60
// Group by a key
const byDecade = users.reduce((acc, u) => {
const key = Math.floor(u.age / 10) * 10;
(acc[key] ||= []).push(u.name);
return acc;
}, {});
// { 30: ['Ada'], 50: ['Linus'] }
Always pass an initial value; without one, an empty array throws a TypeError.
Searching
const nums = [5, 12, 8, 130, 44];
nums.find((n) => n > 10); // 12
nums.findIndex((n) => n > 10); // 1
nums.some((n) => n > 100); // true
nums.every((n) => n > 0); // true
nums.includes(8); // true
find returns undefined when nothing matches; findIndex returns -1.
Sorting without surprises
By default sort() compares elements as strings:
[10, 9, 1].sort(); // [1, 10, 9] (!)
[10, 9, 1].sort((a, b) => a - b); // [1, 9, 10]
sort mutates the array. Use toSorted (ES2023) or copy first: [...arr].sort(...). For text with accents, arr.sort((a, b) => a.localeCompare(b)).
Flattening
[[1, 2], [3, [4]]].flat(); // [1, 2, 3, [4]]
[[1, 2], [3, [4]]].flat(Infinity); // [1, 2, 3, 4]
['a b', 'c'].flatMap((s) => s.split(' ')); // ['a', 'b', 'c']
Mutation: the source of subtle bugs
const a = [1, 2, 3];
const b = a; // same array, not a copy
b.push(4);
console.log(a); // [1, 2, 3, 4]
Create copies with spread ([...a]) or slice(). In frameworks such as React, never mutate state arrays; return a new array via map, filter or spread.
Useful modern additions
at(-1)reads the last item.Array.from({ length: 5 }, (_, i) => i)builds[0, 1, 2, 3, 4].Object.groupBy(items, fn)andstructuredClone(arr)are available in current runtimes.
Working with JSON
API responses are usually arrays of objects. Inspect them with the JSON Formatter, then transform them using the methods above. After reshaping you can export with JSON to CSV. Messy minified code from a bundle? The JavaScript Formatter can make it readable. For text processing inside callbacks, see Regular Expressions for Beginners.
Related tools
JavaScript Formatter
Beautify or minify-whitespace JavaScript code.
JSON Formatter
Format, beautify and minify JSON with custom indentation.
Regex Tester
Test JavaScript regular expressions with live match highlighting.
FAQ
Does map change the original array?
No. map, filter, slice and concat return new arrays. push, pop, splice, sort and reverse mutate in place.
Why does sort() order numbers incorrectly?
By default sort converts elements to strings. Pass a comparator such as (a, b) => a - b for numbers.
When should I use reduce?
When you are folding a list into one value (a sum, an object, a Map). For simple transformations prefer map or filter.
Keep reading
- What Is JSON and How Does It Work?
Learn what JSON is, its syntax rules, data types, common mistakes and how to read and write it in JavaScript and Python.
- Regular Expressions for Beginners
Learn regular expressions from scratch: character classes, quantifiers, anchors, groups, lookaheads, flags and common patterns, with JavaScript examples.