Aral Code

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

Example forEach
<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:

Example Add 10% VAT to every price
<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

Example Passing grades only
<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:

Example Total and average with reduce
<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:

Example Sorting numbers correctly
<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:

Example Top scores, rounded, highest first
<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

MethodReturnsChanges original?
forEachnothingNo
mapnew array, same lengthNo
filternew array, maybe shorterNo
findone item or undefinedNo
some / everytrue / falseNo
reduceone valueNo
sortthe same array, sortedYes

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. Which method keeps only the items that pass a test?
2. What does [1, 2, 3].map(n => n * 10) return?
3. What does [5, 10, 15].reduce((s, n) => s + n, 0) return?
4. Why is [10, 9, 1].sort() wrong for numbers?

By Sir Reyel · Last updated 2026-12-04