JavaScript Array Methods – map(), filter(), reduce() & More
JavaScript arrays come with built-in methods that make data processing easier, faster, and more readable.
1. map() – Transform Each Element
The map() method creates a new array by applying a function to every element.
Example:
const numbers = [1, 2, 3, 4];
const doubled = numbers.map(num => num * 2);
console.log(doubled);
Output
CopyEdit
[2, 4, 6, 8]
Use When: You want to change or transform each item.
2. filter() – Keep Some Elements
The filter() method returns a new array with elements that pass a condition.
Example:
const ages = [10, 18, 21, 15];
const adults = ages.filter(age => age >= 18);
console.log(adults);
Output
CopyEdit
[18, 21]
Use When: You want to remove unwanted items from an array.
3. reduce() – Turn Array into a Single Value
The reduce() method reduces the array to a single value by applying a function on each element.
Example:
const prices = [10, 20, 30];
const total = prices.reduce((sum, price) => sum + price, 0);
console.log(total);
Output
60
Use When: You need to sum, multiply, or accumulate values.
4. forEach() – Loop Over Elements
The forEach() method executes a function for each item, but doesn’t return anything.
Example:
const fruits = ["apple", "banana", "cherry"];
fruits.forEach(fruit => console.log("I like", fruit));
Output
I like banana
I like cherry
Use When: You need a simple loop with side effects (e.g., printing, DOM changes).
5. find() – Get First Match
The find() method returns the first element that matches a condition.
Example:
const users = [{id: 1}, {id: 2}, {id: 3}];
const user = users.find(u => u.id === 2);
console.log(user);
Output
CopyEdit
{id: 2}
Use When: You want just the first match.
6. some() and every()
some() → Returns true if any element matches
every() → Returns true if all elements match
Example:
const scores = [70, 80, 90];
console.log(scores.some(score => score < 60)); // false
console.log(scores.every(score => score >= 70)); // true
7. includes() – Check if Array Contains Value
Example:
const colors = ["red", "green", "blue"];
console.log(colors.includes("green")); // true
console.log(colors.includes("yellow")); // false
8. sort() – Sort Array Elements
Be careful — sort() modifies the original array.
Example:
const nums = [3, 1, 4, 2];
nums.sort(); // Default sort is alphabetical
console.log(nums); // [1, 2, 3, 4]
nums.sort((a, b) => b - a); // Descending
console.log(nums); // [4, 3, 2, 1]
9. splice() and slice()
- splice() → Modifies array (add/remove)
- slice() → Returns copy of array portion
Example:
const items = ['a', 'b', 'c', 'd'];
const removed = items.splice(1, 2); // Removes 2 items from index 1
console.log(removed); // ['b', 'c']
const copy = items.slice(0, 2);
console.log(copy); // ['a', 'd']
Summary Table: Array Methods
Method | Purpose | Returns New Array? | Mutates Original? |
---|---|---|---|
map() | Transform items | Yes | No |
filter() | Keep items matching rule | Yes | No |
reduce() | Return single value | No (value only) | No |
forEach() | Loop through items | No | No |
find() | First match | No | No |
some() | Any match? | No (boolean) | No |
every() | All match? | No (boolean) | No |
includes() | Value exists? | No (boolean) | No |
sort() | Sort items | No (modifies) | Yes |
splice() | Remove/insert items | No | Yes |
slice() | Copy portion | Yes | No |