Exploring Higher Order Functions in JavaScript Arrays
A function is a HOF if any of the condition is true -
It accepts another function as an argument.
It returns a function
Why to use HOF?
HOF are essential for writing clean, reusable, and maintainable code. They allow us to abstract common operations like transforming, filtering, or reducing data into reusable functions that accept other functions as arguments.
How an HOF looks like?
const sum = (a,b) => {
return a+b;
}
const diff = (a,b) => {
return a-b;
}
const calculate = (func, a, b) => {
return func(a,b)
}
console.log(calculate(sum, 2, 5));
Here in the above example, the calculate function is a HOF as it accepts a function in arguments.
Important things to know of HOF
All these methods do not deep copy
They work on references
Objects inside arrays remain the same objects
Commonly Used HOF in JavaScript
map()
It accepts - element, index, array
It returns - array of same length
Use case
It is used for Data transformation.
Time Complexity
O(n)
Example
const double = (x) => x * 2; const transform = (func, arr) => { return arr.map(func); }; transform(double, [1, 2, 3]); // [2, 4, 6]Can mutate elements inside - Yes
filter()
It accepts - element, index, array
It returns - New array (variable length)
Use case
Search results
Removing invalid data.
Time Complexity
O(n)
Example
const isEven = (x) => x % 2 === 0; const select = (func, arr) => { return arr.filter(func); }; select(isEven, [1,2,3,4]); // [2,4]Can mutate elements inside - Yes
reduce()
It accepts - accumulator(the value from previous callback function call, currentValue, index, array
It returns - Number, Object, Array, String (Any of these)
Use case
Sum / product
Grouping data
Frequency counts
Time Complexity
O(n)
Example
const sumReducer = (acc, curr) => acc + curr; const calculate = (func, arr, init) => { return arr.reduce(func, init); }; calculate(sumReducer, [1,2,3], 0); // 6Can mutate elements inside - Yes
find()
It accepts - element, index, array
It returns - Single element or undefined
Use case
Finding a unique value
Finding the first value that matches a certain condition
Time Complexity
O(n)
Best case O(1) - It stops early if the condition is met
Example
const greaterThan5 = (x) => x > 5; const findOne = (func, arr) => { return arr.find(func); }; findOne(greaterThan5, [1,2,4,5,7,6,8]); // 7 //First element that mathces the condition is returnedCan mutate elements inside - Yes
forEach()
It accepts - element, index, array
It returns - Nothing (As it is used for accessing the data elements one by one)
Use case
Logging
API calls
Time Complexity
O(n)
Example
const log = (x) => console.log(x); const process = (func, arr) => { arr.forEach(func); }; process(log, [1, 2, 3]);Can mutate elements inside - Yes
every()
It accepts - element, index, array
It returns - Boolean
Use case
Validation
Time Complexity
O(n)
Example
const isPositive = (x) => x > 0; const checkAll = (func, arr) => { return arr.every(func); }; console.log(checkAll(isPositive, [1, 2, 3])); //returns true as all elements are greater than 0Can mutate elements inside - No
some()
Similar to every(), returns true even if any element matches the condition.
It accepts - element, index, array
It returns - Boolean
Use case
Search flags
Time Complexity
O(n)
Example
const hasEven = (x) => x % 2 === 0; const checkAny = (func, arr) => { return arr.some(func); }; checkAny(hasEven, [1,3,4]); //returns trueCan mutate elements inside - No