JavaScript Array Methods Tutorial: map, filter, reduce, find, some and every with Examples

Arrays are the default list type in JavaScript, and most of the day-to-day work you do with them — transform items, keep only some of them, compute a total, find the first match — is easier with the built-in array methods than with handwritten for loops. Once you are comfortable with map, filter, reduce, find, some and every, reading other people's code (and writing your own API responses) gets much faster.

In this JavaScript Array Methods tutorial you will run four small Node.js scripts that print real output. Download the zip below, extract it, and run each file with node 1_map_filter.js.

Requirements

  • Node.js 18 or newer (any recent LTS is fine), or a browser console.
  • A code editor such as VS Code.
  • Basic JavaScript: arrays, objects, arrow functions.

map and filter: Transform and Keep

map returns a new array with one result per item. filter returns a new array with only the items that pass a test. They do not change the original array. Save as 1_map_filter.js:

  1. const students = [
  2.   { name: "Ana", score: 92 },
  3.   { name: "Ben", score: 67 },
  4.   { name: "Cia", score: 81 },
  5.   { name: "Dan", score: 55 },
  6.   { name: "Eve", score: 88 },
  7. ];
  8.  
  9. // map: transform every item
  10. const names = students.map((s) => s.name);
  11. console.log("names:", names);
  12.  
  13. const grades = students.map((s) => ({
  14.   name: s.name,
  15.   passed: s.score >= 75,
  16. }));
  17. console.log("grades:", grades);
  18.  
  19. // filter: keep only items that match
  20. const passers = students.filter((s) => s.score >= 75);
  21. console.log(
  22.   "passers:",
  23.   passers.map((s) => s.name)
  24. );
  25.  
  26. // chain map + filter
  27. const honorNames = students
  28.   .filter((s) => s.score >= 90)
  29.   .map((s) => s.name.toUpperCase());
  30. console.log("honor roll:", honorNames);

Run node 1_map_filter.js. Output:

names: [ 'Ana', 'Ben', 'Cia', 'Dan', 'Eve' ]
grades: [
  { name: 'Ana', passed: true },
  { name: 'Ben', passed: false },
  { name: 'Cia', passed: true },
  { name: 'Dan', passed: false },
  { name: 'Eve', passed: true }
]
passers: [ 'Ana', 'Cia', 'Eve' ]
honor roll: [ 'ANA' ]

Chaining is common: filter first (smaller list), then map (cheaper transform). Here only Ana scored 90+, so the honor roll is ['ANA'].

reduce: Turn a List into One Value

reduce walks the array and carries an accumulator. Use it for sums, grouping and frequency maps. Save as 2_reduce.js:

  1. const cart = [
  2.   { item: "USB cable", price: 120, qty: 2 },
  3.   { item: "Mouse", price: 450, qty: 1 },
  4.   { item: "Notebook", price: 55, qty: 3 },
  5. ];
  6.  
  7. // sum of (price * qty)
  8. const total = cart.reduce((sum, row) => sum + row.price * row.qty, 0);
  9. console.log("cart total:", total);
  10.  
  11. // group scores by pass/fail
  12. const scores = [92, 67, 81, 55, 88, 74, 95];
  13. const grouped = scores.reduce(
  14.   (acc, n) => {
  15.     (n >= 75 ? acc.pass : acc.fail).push(n);
  16.     return acc;
  17.   },
  18.   { pass: [], fail: [] }
  19. );
  20. console.log("grouped:", grouped);
  21.  
  22. // count word frequency
  23. const words = ["php", "js", "php", "python", "js", "php"];
  24. const freq = words.reduce((acc, w) => {
  25.   acc[w] = (acc[w] || 0) + 1;
  26.   return acc;
  27. }, {});
  28. console.log("frequency:", freq);

Output:

cart total: 855
grouped: { pass: [ 92, 81, 88, 95 ], fail: [ 67, 55, 74 ] }
frequency: { php: 3, js: 2, python: 1 }

The second argument to reduce is the starting accumulator (0, { pass: [], fail: [] }, or {}). Forgetting it is a frequent source of bugs when the array might be empty.

find, some and every

These answer different questions:

  • find — the first matching item (or undefined)
  • some — true if any item matches
  • every — true if all items match

Save as 3_find_some_every.js:

  1. const products = [
  2.   { id: 1, name: "Keyboard", stock: 12, price: 899 },
  3.   { id: 2, name: "Monitor", stock: 0, price: 4500 },
  4.   { id: 3, name: "Webcam", stock: 4, price: 1200 },
  5.   { id: 4, name: "Headset", stock: 7, price: 650 },
  6. ];
  7.  
  8. // find: first match (or undefined)
  9. const soldOut = products.find((p) => p.stock === 0);
  10. console.log("first sold out:", soldOut ? soldOut.name : "none");
  11.  
  12. const cheap = products.find((p) => p.price < 500);
  13. console.log("under 500:", cheap ? cheap.name : "none");
  14.  
  15. // findIndex
  16. const idx = products.findIndex((p) => p.id === 3);
  17. console.log("webcam index:", idx);
  18.  
  19. // some: true if ANY item matches
  20. console.log("any sold out?", products.some((p) => p.stock === 0));
  21. console.log("any over 5000?", products.some((p) => p.price > 5000));
  22.  
  23. // every: true if ALL items match
  24. console.log("all in stock?", products.every((p) => p.stock > 0));
  25. console.log("all under 10000?", products.every((p) => p.price < 10000));
  26.  
  27. // practical: can fulfill an order?
  28. const orderQty = 5;
  29. const webcam = products.find((p) => p.name === "Webcam");
  30. const canShip = webcam && webcam.stock >= orderQty;
  31. console.log(`can ship ${orderQty} webcams?`, canShip);

Output:

first sold out: Monitor
under 500: none
webcam index: 2
any sold out? true
any over 5000? false
all in stock? false
all under 10000? true
can ship 5 webcams? false

Prefer some/every when you only need a yes/no answer — they can stop early. Prefer find when you need the object itself.

Practical Capstone Example

A small enrollment report that filters active Web Dev students, computes the average, finds the top scorer and builds a pass/fail list. Save as 4_practical.js:

  1. // Capstone-style: filter enrolled students, compute average, top scorer
  2. const enrollments = [
  3.   { student: "Ana", course: "Web Dev", score: 92, active: true },
  4.   { student: "Ben", course: "Web Dev", score: 67, active: true },
  5.   { student: "Cia", course: "Database", score: 81, active: false },
  6.   { student: "Dan", course: "Web Dev", score: 55, active: true },
  7.   { student: "Eve", course: "Web Dev", score: 88, active: true },
  8.   { student: "Fay", course: "Database", score: 95, active: true },
  9. ];
  10.  
  11. const webDevActive = enrollments.filter(
  12.   (e) => e.course === "Web Dev" && e.active
  13. );
  14. console.log(
  15.   "active Web Dev:",
  16.   webDevActive.map((e) => e.student)
  17. );
  18.  
  19. const avg =
  20.   webDevActive.reduce((sum, e) => sum + e.score, 0) / webDevActive.length;
  21. console.log("Web Dev average:", avg.toFixed(1));
  22.  
  23. const top = webDevActive.reduce((best, e) =>
  24.   e.score > best.score ? e : best
  25. );
  26. console.log("top student:", top.student, top.score);
  27.  
  28. const anyoneFailing = webDevActive.some((e) => e.score < 60);
  29. console.log("anyone failing?", anyoneFailing);
  30.  
  31. const allPassing = webDevActive.every((e) => e.score >= 50);
  32. console.log("all at least 50?", allPassing);
  33.  
  34. // Build a report list with map
  35. const report = webDevActive.map((e) => ({
  36.   name: e.student,
  37.   grade: e.score >= 75 ? "Passed" : "Failed",
  38.   score: e.score,
  39. }));
  40. console.log("report:", report);

Output:

active Web Dev: [ 'Ana', 'Ben', 'Dan', 'Eve' ]
Web Dev average: 75.5
top student: Ana 92
anyone failing? true
all at least 50? true
report: [
  { name: 'Ana', grade: 'Passed', score: 92 },
  { name: 'Ben', grade: 'Failed', score: 67 },
  { name: 'Dan', grade: 'Failed', score: 55 },
  { name: 'Eve', grade: 'Passed', score: 88 }
]

This is the same shape of code you will write for dashboards: filter the rows that matter, reduce for totals, map into a view model for the table.

Quick Comparison

MethodReturnsTypical use
mapNew array (same length)Format rows for a UI or API
filterNew array (0..n items)Search results, active records
reduceAny single valueSum, group, index by id
find / findIndexItem or indexLookup by id
some / everyBooleanValidation, permissions checks

Common Mistakes

  • Using map when you meant forEach (or the other way around). map builds a new array; if you ignore the return value, use forEach.
  • Mutating objects inside map and wondering why the original list changed — clone fields you change.
  • Calling find and immediately reading a property without checking for undefined.
  • Writing a giant reduce when a simple filter + map chain is clearer.

Conclusion

Mastering map, filter, reduce, find, some and every covers most array work in front-end and Node.js projects. Practice with the four scripts in the download, then rewrite one of your old for loops as a chain and compare readability. Once these methods feel natural, the rest of the array toolkit (flatMap, sort, includes) slots in easily.

Download the complete example scripts below and run them with Node.js. Happy coding!