Do you need to transform a list, find one item or check whether something exists? Choosing an array method starts with the result you need.
Here are 15 useful JavaScript array methods, grouped by task, with short examples.
Transform values or perform an action
1. map(): Transform items
Returns a new array containing the callback’s result for each item.
[2, 4, 6].map(number => number * 2);
// [4, 8, 12]
2. forEach(): Perform an action
Runs a callback for each item. The method returns undefined, so use it when you do not need a transformed array.
["HTML", "CSS"].forEach(skill => {
console.log(skill);
});
3. flatMap(): Transform and flatten one level
["web development", "API testing"]
.flatMap(topic => topic.split(" "));
// ["web", "development", "API", "testing"]
Find or select items
4. filter(): Keep matching items
Returns all matches in a new array, or an empty array when nothing matches.
[5, 12, 18].filter(number => number > 10);
// [12, 18]
5. find(): Get the first match
Returns the first matching item, or undefined if no match exists.
[5, 12, 18].find(number => number > 10);
// 12
6. includes(): Check for a value
["JavaScript", "C#"].includes("C#");
// true
For objects, includes() checks identity, not matching property values. Use a predicate-based method when searching by a property.
7. indexOf(): Find a value’s position
Returns the first matching index, or -1 when the value is absent.
["HTML", "CSS", "JavaScript"].indexOf("CSS");
// 1
8. every(): Check all items
Returns whether every item passes the condition. An empty array returns true, so check length separately when emptiness should be invalid.
[4, 8, 12].every(number => number > 0);
// true
Copy, combine or summarise
9. slice(): Copy a section
The end index is excluded. The original array is unchanged.
["a", "b", "c", "d"].slice(1, 3);
// ["b", "c"]
10. concat(): Combine arrays
["HTML", "CSS"].concat(["JavaScript"]);
// ["HTML", "CSS", "JavaScript"]
11. join(): Create a string
Combines the items into a string using your chosen separator.
["Git", "React", "SQL"].join(", ");
// "Git, React, SQL"
12. reduce(): Accumulate a result
Useful for tasks such as calculating a total. Supplying an initial value also handles an empty array predictably.
[10, 20, 30].reduce(
(total, price) => total + price,
0
);
// 60
Modify an existing array
13. push(): Append items
Changes the array and returns its new length.
const skills = ["HTML"];
const length = skills.push("CSS");
console.log(skills); // ["HTML", "CSS"]
console.log(length); // 2
14. splice(): Remove or insert items
Changes the original array and returns the removed items.
const letters = ["a", "b", "c"];
const removed = letters.splice(1, 1, "x");
console.log(letters); // ["a", "x", "c"]
console.log(removed); // ["b"]
15. sort(): Reorder items
Changes the original array. Supply a comparator for numeric sorting because the default order compares string representations.
const numbers = [10, 2, 30];
numbers.sort((a, b) => a - b);
// numbers is now [2, 10, 30]
Use toSorted() when you need a sorted copy instead.
Two checks before choosing
- What should come back? A matching item, boolean, string, total or new array?
- Can the original change? Avoid directly using mutating methods on React state or shared data that should remain unchanged.
A new array is not necessarily a deep copy. Methods such as filter() and slice() can retain references to the original objects inside it. Changing those objects can still affect shared data.
Further reading: MDN: JavaScript Array reference.
No comments