Arrays & Objects Lesson 19 of 30
JavaScript Array Methods — forEach, map, filter, find, reduce and sort
Process lists the modern way with forEach, map, filter, find, some, every, reduce and sort, using class grades and store prices as examples.
You can process arrays with for loops, but JavaScript has built-in array methods that do common jobs in one clear line. Each one takes a callback, a small function that runs for every item.
forEach: do something with each item
<pre id="out"></pre>
<script>
const names = ["Ana", "Ben", "Carla"];
let log = "";
names.forEach((name, index) => {
log += `${index + 1}. ${name}\n`;
});
document.getElementById("out").textContent = log;
</script>
The callback receives the item and, optionally, its index.
map: transform every item
map makes a new array by changing each item:
<pre id="out"></pre>
<script>
const prices = [100, 250, 40];
const withVat = prices.map((p) => p * 1.12);
document.getElementById("out").textContent =
"Before: " + prices.join(", ") + "\nAfter: " + withVat.map((p) => p.toFixed(2)).join(", ");
</script>
The new array always has the same number of items as the original.
filter: keep only some items
<pre id="out"></pre>
<script>
const grades = [88, 72, 95, 60, 79];
const passing = grades.filter((g) => g >= 75);
document.getElementById("out").textContent =
"All: " + grades.join(", ") + "\nPassing: " + passing.join(", ");
</script>
The callback returns true to keep an item and false to drop it.
find and findIndex
find returns the first item that matches (or undefined). findIndex returns its position (or -1).
const grades = [88, 72, 95, 60];
grades.find((g) => g < 75) // 72
grades.findIndex((g) => g > 90) // 2
some and every
const grades = [88, 72, 95];
grades.some((g) => g < 75) // true — at least one failing grade
grades.every((g) => g >= 70) // true — all grades are 70 or more
reduce: combine everything into one value
reduce walks through the array and builds up a single result, like a total:
<pre id="out"></pre>
<script>
const grades = [88, 91, 85, 93];
const total = grades.reduce((sum, g) => sum + g, 0);
const average = total / grades.length;
document.getElementById("out").textContent =
"Total: " + total + "\nAverage: " + average.toFixed(2);
</script>
sum starts at 0 (the second argument). For each grade, the callback returns the new sum, which is passed into the next round.
sort
sort() arranges items, but by default it sorts as text, which gives strange results for numbers:
<pre id="out"></pre>
<script>
const nums = [100, 25, 3, 40];
const wrong = [...nums].sort();
const right = [...nums].sort((a, b) => a - b);
const highFirst = [...nums].sort((a, b) => b - a);
document.getElementById("out").textContent =
"sort(): " + wrong.join(", ") +
"\nsort(a - b): " + right.join(", ") +
"\nsort(b - a): " + highFirst.join(", ");
</script>
Chaining methods
Because map and filter return new arrays, you can chain them into a pipeline:
<pre id="out"></pre>
<script>
const scores = [88.4, 72.9, 95.6, 60.2, 91.5];
const topScores = scores
.filter((s) => s >= 85)
.map((s) => Math.round(s))
.sort((a, b) => b - a);
document.getElementById("out").textContent = topScores.join(", ");
</script>
Quick reference
| Method | Returns | Changes original? |
|---|---|---|
forEach | nothing | No |
map | new array, same length | No |
filter | new array, maybe shorter | No |
find | one item or undefined | No |
some / every | true / false | No |
reduce | one value | No |
sort | the same array, sorted | Yes |
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04