JavaScript Array Methods Every Developer Needs
JavaScript's built-in array methods are some of the most powerful tools in your toolkit. Instead of writing verbose loops, these methods let you express intent clearly and concisely. Below are 10 essential snippets you can drop directly into your projects.
Table of Contents
1. Transforming Arrays with map()
map() creates a new array by applying a function to every element. The original array is never mutated.
// Double every number in an array
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(n => n * 2);
// [2, 4, 6, 8, 10]
// Extract a single property from an array of objects
const users = [{ name: 'Alice', age: 30 }, { name: 'Bob', age: 25 }];
const names = users.map(user => user.name);
// ['Alice', 'Bob']
2. Filtering Arrays with filter()
filter() returns a new array containing only elements that pass a test function.
// Keep only even numbers
const nums = [1, 2, 3, 4, 5, 6];
const evens = nums.filter(n => n % 2 === 0);
// [2, 4, 6]
// Filter objects by a property value
const activeUsers = users.filter(user => user.isActive === true);
3. Aggregating Data with reduce()
reduce() is the Swiss Army knife of array methods. It processes all elements and returns a single accumulated value.
// Sum all numbers
const total = [10, 20, 30].reduce((acc, val) => acc + val, 0);
// 60
// Group objects by a property
const people = [
{ name: 'Alice', dept: 'Engineering' },
{ name: 'Bob', dept: 'Design' },
{ name: 'Carol', dept: 'Engineering' },
];
const byDept = people.reduce((acc, person) => {
const key = person.dept;
if (!acc[key]) acc[key] = [];
acc[key].push(person);
return acc;
}, {});
// { Engineering: [...], Design: [...] }
4. Searching with find() and findIndex()
Use find() to retrieve the first matching element, and findIndex() to get its position.
const products = [
{ id: 1, name: 'Keyboard' },
{ id: 2, name: 'Monitor' },
];
const monitor = products.find(p => p.id === 2);
// { id: 2, name: 'Monitor' }
const monitorIndex = products.findIndex(p => p.id === 2);
// 1
5. Flattening Nested Arrays with flat() and flatMap()
flat() collapses nested arrays, while flatMap() combines mapping and flattening in one step.
const nested = [1, [2, 3], [4, [5, 6]]];
nested.flat(); // [1, 2, 3, 4, [5, 6]]
nested.flat(2); // [1, 2, 3, 4, 5, 6]
// flatMap: map then flatten one level
const sentences = ['Hello World', 'Foo Bar'];
const words = sentences.flatMap(s => s.split(' '));
// ['Hello', 'World', 'Foo', 'Bar']
6. Testing Arrays with every() and some()
every() returns true if all elements pass the test. some() returns true if at least one element passes.
const ages = [22, 31, 19, 45];
ages.every(age => age >= 18); // true — all adults
ages.some(age => age >= 40); // true — at least one is 40+
ages.every(age => age >= 21); // false — 19 fails the test
Quick Reference Table
| Method | Returns | Mutates Original? |
|---|---|---|
| map() | New array (same length) | No |
| filter() | New array (shorter/equal) | No |
| reduce() | Single value | No |
| find() | First matching element | No |
| findIndex() | Index number | No |
| flat() | New flattened array | No |
| every() | Boolean | No |
| some() | Boolean | No |
None of these methods mutate the original array — making them safe to use in functional-style and React-based codebases. Bookmark this page and reach for these snippets whenever you need to manipulate data efficiently.