Working with arrays in JavaScript? You’re going to run into methods like map
, filter
, and reduce
—and if you know how to use them well, they can make your code cleaner, faster, and more expressive.
🧠 Why learn these methods?
Array methods let you transform, filter, and summarize data in just a few lines of code.
Instead of writing loops manually, you can use built-in methods that are easier to read, debug, and chain together.
🔄 Map()
Use it when: you want to transform every element in an array.
It returns a new array, leaving the original untouched.
const names = ['Marta', 'Luis', 'Ana'];
const uppercased = names.map(name => name.toUpperCase());
// ['MARTA', 'LUIS', 'ANA']
🔸 Common use case: formatting data before rendering in the UI.
✅ Filter()
Use it when: you want to keep only certain elements that match a condition.
const ages = [15, 22, 18, 30];
const adults = ages.filter(age => age >= 18);
// [22, 18, 30]
🔸 Common use case: showing only active users, products in stock, or valid inputs.
➕ Reduce()
Use it when: you want to calculate a single value from an array.
Example: sum all numbers in an array.
const numbers = [5, 10, 15];
const total = numbers.reduce((acc, curr) => acc + curr, 0);
// 30
You can use reduce
to:
- Sum totals
- Count items
- Flatten arrays
- Build objects or groups
🔸 It’s powerful—but can be harder to read if misused.
🔁 Foreach()
Use it when: you want to run a function on each item, but don’t need to return anything.
const fruits = ['apple', 'banana'];
fruits.forEach(fruit => {
console.log(`I like ${fruit}`);
});
⚠️ forEach
doesn’t return a new array—use it for side effects like logging or API calls.
🧪 Some() and every()
These two methods are about conditions.
some()
returnstrue
if at least one item passes the testevery()
returnstrue
if all items pass
const scores = [90, 80, 100];
scores.every(score => score >= 50); // true
scores.some(score => score === 100); // true
🔍 Find() and findindex()
Use these to get a single item from an array.
const users = [
{ id: 1, name: 'Marta' },
{ id: 2, name: 'Luis' }
];
const user = users.find(u => u.id === 2);
// { id: 2, name: 'Luis' }
const index = users.findIndex(u => u.name === 'Marta');
// 0
🧩 Real-world example: filtering and mapping
Let’s say you have a list of users and want to show only verified names in uppercase:
const users = [
{ name: 'Marta', verified: true },
{ name: 'Pedro', verified: false },
{ name: 'Ana', verified: true }
];
const verifiedNames = users
.filter(user => user.verified)
.map(user => user.name.toUpperCase());
// ['MARTA', 'ANA']
This kind of chaining is common in frontend apps (like React) and backend APIs (like Express).
✅ Summary checklist
Method | Purpose |
map() | Transform each element |
filter() | Keep elements that match a test |
reduce() | Turn array into a single value |
forEach() | Run a side-effect per element |
some() | Check if any element matches |
every() | Check if all match |
find() | Get the first matching item |
findIndex() | Get index of match |
🧠 Conclusion
JavaScript array methods is a game-changer for writing cleaner, more functional, and more readable code.
These methods don’t just replace for
loops—they make your intentions clear: Are you transforming? Filtering? Summarizing? You say it with the method you choose.
Once you start using them, you’ll wonder how you ever wrote JavaScript without them.