⚙️ Core Higher-Order Array Methods in JavaScript
Last Updated: 22nd October 2025
Higher-Order Functions are functions that take another function as an argument (called a callback) or return a function.
In arrays, these built-in methods help to iterate, transform, or reduce data efficiently — without writing loops manually.
Hinglish Tip 🗣: Ye methods “smart loops” ki tarah kaam karte hain — aapko
foryawhilelikhne ki zarurat nahi hoti, sab kuch ek line me ho jaata hai!
forEach() — Simple Loop for Action
Executes a function once for each element in the array.
Syntax
array.forEach(function (currentValue, index, array) {
// code to execute
});
let fruits = ["apple", "banana", "cherry"];
fruits.forEach(function (item, index) {
console.log(index + ": " + item);
});
// Output:
// 0: apple
// 1: banana
// 2: cherry
map() — Loop for Transformation
Creates a new array by applying a function to each element of the original.
Syntax
array.map(function (currentValue, index, array) {
return newValue;
});
let numbers = [1, 2, 3];
let doubled = numbers.map((num) => num * 2);
console.log(doubled);
// Output: [2, 4, 6]
filter() — Select Elements Matching Condition
Returns a new array containing only elements that satisfy a condition.
Syntax
array.filter(function (currentValue, index, array) {
return condition; // true or false
});
let marks = [85, 42, 77, 90, 58];
let passed = marks.filter((score) => score >= 60);
console.log(passed);
// Output: [85, 77, 90]
reduce() — Combine All Values into One
Used to accumulate all elements into a single value (sum, average, etc.)
Syntax
array.reduce(function (accumulator, currentValue, index, array) {
return updatedAccumulator;
}, initialValue);
let numbers = [1, 2, 3, 4, 5];
let sum = numbers.reduce((acc, num) => acc + num, 0);
console.log(sum);
// Output: 15
find() — Find First Element Matching Condition
Returns the first element that satisfies the condition, otherwise undefined.
Syntax
array.find(function (currentValue, index, array) {
return condition;
});
let ages = [12, 18, 25, 30];
let adult = ages.find((age) => age >= 18);
console.log(adult);
// Output: 18
findIndex() — Find Index of First Element Matching Condition
Returns the index of the first element that satisfies the given condition.If no match is found, it returns -1.
let nums = [10, 25, 30, 45];
let index = nums.findIndex((num) => num > 20);
console.log(index);
// Output: 2
some() — Check if Any Element Satisfies Condition
Returns true if at least one element satisfies the condition, otherwise false.
let scores = [45, 60, 33, 90];
let hasPassed = scores.some((score) => score >= 50);
console.log(hasPassed);
// Output: true
every() — Check if All Elements Satisfy Condition
Returns true if all elements satisfy the condition, otherwise false.
let marks = [70, 80, 90];
let allPassed = marks.every((score) => score >= 60);
console.log(allPassed);
// Output: true
sort() — Sort Elements in an array
Sorts array elements in place (modifies original array).By default, it sorts elements as strings, not numbers!
1: Sorting Strings
let fruits = ["banana", "apple", "cherry"];
fruits.sort();
console.log(fruits);
// Output: ["apple", "banana", "cherry"]
2: Sorting Numbers
let numbers = [100, 25, 9];
numbers.sort((a, b) => a - b);
console.log(numbers);
// Output: [9, 25, 100]
Hinglish Tip 🗣: sort() bina compare function ke string sort karta hai “25” ko “100” se bada samjhta hai!
flat() — Flatten Nested Arrays
Flattens nested arrays up to a given depth.
let matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
];
let flattened = matrix.flat();
console.log(flattened);
// Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Hinglish Tip 🗣: flat() nested array ko “seedha” bana deta hai — jaise folders ke andar folder ko ek saath khol do.
flatMap() — Flatten Nested Arrays with Mapping
First applies a map function, then flattens the result by one level.
let nums = [1, 2, 3];
let result = nums.flatMap(x => [x, x * 2]);
console.log(result);
// Output: [1, 2, 2, 4, 3, 6]
💡 Quick Practice
- Use map() to convert all temperatures from Celsius to Fahrenheit.
- Use filter() to remove words shorter than 4 letters.
- Use reduce() to find the average of [10, 20, 30, 40].
- Use find() to get the first negative number in [5, -3, 7, -1].
- Use some() to check if any element is divisible by 5.
- Use every() to check if all ages are above 18.