Posted on: 30/05/2026(updated)
Arrays are one of the most commonly used data structures in JavaScript. While they offer many useful built-in methods to help developers manipulate and work with data, map(), filter(), and reduce() are probably the most important, as well as essential tools for functional programming in JavaScript.
Concept: Used when you want to look at every single item in an array, modify or extract something from it, and return a new array of the exact same length.
Imagine we start with this original array of song objects:
const songs = [
{ name: 'The Motherload', artist: 'Mastodon', duration: 300 },
{ name: 'Blood and Thunder', artist: 'Mastodon', duration: 228 },
{ name: 'The Trooper', artist: 'Iron Maiden', duration: 253 }
];
const songNames = songs.map(song => song.name);
// Output: ['The Motherload', 'Blood and Thunder', 'The Trooper']
const lowercaseSongs = songs.map(song => {
return {
...song,
name: song.name.toLowerCase()
};
});
Concept: Used when you want to evaluate every item in an array and selectively choose which ones to keep based on a condition.
Using the same songs array from above:
const numbers = [1, 2, 3, 4, 5, 6];
const evenNumbers = numbers.filter(num => num % 2 === 0);
// Output: [2, 4, 6]
const mastodonSongs = songs.filter(song => song.artist === 'Mastodon');
/* Output:
[
{ name: 'The Motherload', artist: 'Mastodon', duration: 300 },
{ name: 'Blood and Thunder', artist: 'Mastodon', duration: 228 }
]
*/
Concept: Used when you want to take an array of values and combine ("reduce") them into a single output value (such as a single number, an object, or a flattened array).
const numbers = [10, 20, 30, 40];
const totalSum = numbers.reduce((accumulator, currentValue) =>
accumulator + currentValue, 0);
// totalSum = 100
const average = totalSum / numbers.length; // 25
const artistCounts = songs.reduce((acc, song) => {
acc[song.artist] = (acc[song.artist] || 0) + 1;
return acc;
}, {});
// Output: { 'Mastodon': 2, 'Iron Maiden': 1 }
Because map and filter return new arrays, you can string (chain) these methods together consecutively to perform highly complex operations in a clean, readable layout.
Imagine you have a nested array of songs from different music platforms (Spotify and LastFM), and you want to get a comma-separated string of names for all songs that are longer than 3 minutes (180 seconds):
const spotifySongs = [
{ name: 'The Motherload', duration: 300 },
{ name: 'Short Track', duration: 90 }
];
const lastFmSongs = [
{ name: 'The Trooper', duration: 253 }
];
// 1. Flatten into a single array using reduce
const allSongs = [spotifySongs, lastFmSongs].reduce((acc, currentArray) => {
return acc.concat(currentArray);
}, []);
// 2. Chain filter and map to get the final result
const longSongNamesString = allSongs
.filter(song => song.duration > 180) // Keeps only songs > 180s
.map(song => song.name) // Extracts just the names
.join(', '); // Joins them into a string
console.log(longSongNamesString);
// Output: "The Motherload, The Trooper"