Published by Hashan Madhushanka

- 15 min read

JavaScript Higher Order Array Methods

js
JavaScript Higher Order Array Methods

If you’re learning JavaScript, you’ve probably heard about array methods like map(), filter(), and forEach(). These are called higher order array methods, and they’re incredibly useful tools that will make your code cleaner and easier to read.

What Are Higher Order Array Methods?

Simply put, these are methods that take a function as an argument and perform operations on arrays. Instead of writing loops manually, you can use these built-in methods to work with your data more efficiently.

Let’s explore the most common ones with easy examples!

1. forEach() – Do Something With Each Item

The forEach() method runs a function on each item in an array. Think of it as a simple way to loop through your array.



const fruits = ['apple', 'banana', 'orange'];

fruits.forEach(function(fruit) {
  console.log(fruit);
});

// Output:
// apple
// banana
// orange

When to use it: When you want to do something with each item but don’t need to create a new array.

2. map() – Transform Your Array

The map() method creates a new array by transforming each item. It’s perfect when you want to change or format your data.



const numbers = [1, 2, 3, 4];

const doubled = numbers.map(function(number) {
  return number * 2;
});

console.log(doubled); // [2, 4, 6, 8]

When to use it: When you need to transform each item and get a new array with the results.

3. filter() – Keep Only What You Need

The filter() method creates a new array with only the items that pass a test. It’s like a bouncer that only lets certain items through.



const ages = [12, 18, 25, 16, 30, 14];

const adults = ages.filter(function(age) {
  return age >= 18;
});

console.log(adults); // [18, 25, 30]

When to use it: When you want to remove items that don’t meet certain conditions.

4. find() – Get the First Match

The find() method returns the first item that matches your condition.



const users = [
  { name: 'John', age: 25 },
  { name: 'Sarah', age: 30 },
  { name: 'Mike', age: 35 }
];

const user = users.find(function(user) {
  return user.name === 'Sarah';
});

console.log(user); // { name: 'Sarah', age: 30 }

When to use it: When you need to find one specific item in an array.

5. reduce() – Combine Everything Into One Value

The reduce() method combines all items in an array into a single value. It’s a bit trickier but very powerful.



const prices = [10, 20, 30, 40];

const total = prices.reduce(function(sum, price) {
  return sum + price;
}, 0);

console.log(total); // 100

The 0 at the end is the starting value for sum.

When to use it: When you need to calculate a single value from all items, like totals or averages.

6. some() – Check If At Least One Item Matches

The some() method checks if at least one item passes your test. It returns true or false.



const numbers = [1, 3, 5, 7, 8];

const hasEven = numbers.some(function(number) {
  return number % 2 === 0;
});

console.log(hasEven); // true (because 8 is even)

7. every() – Check If All Items Match

The every() method checks if all items pass your test.


const numbers = [2, 4, 6, 8];

const allEven = numbers.every(function(number) {
  return number % 2 === 0;
});

console.log(allEven); // true

Using Arrow Functions (Shorter Syntax)

You can make these methods even shorter using arrow functions:


// Regular function
const doubled = numbers.map(function(num) {
  return num * 2;
});

// Arrow function (shorter!)
const doubled = numbers.map(num => num * 2);

Quick Comparison

Method Returns Use Case
forEach() Nothing (undefined) Do something with each item
map() New array Transform items
filter() New array Remove unwanted items
find() Single item Find one specific item
reduce() Single value Combine all items
some() Boolean Check if at least one matches
every() Boolean Check if all match

Final Tips

  1. These methods don’t change the original array (except forEach())
  2. They make your code more readable and professional