Higher Order Functions in JavaScript
A higher-order function is a function that does one of the following:
- Takes another function as an argument.
- Returns another function as its result.
Higher-order functions help make your code more reusable and modular by allowing you to work with functions like any other value.
fun2 is a higher-order function because it takes another function (action) as an argument.
It calls the action function twice.
Popular Higher Order Functions in JavaScript
- map The map function is used to transform an array by applying a callback function to each element. It returns a new array.
- filter The filter function is used to create a new array containing elements that satisfy a given condition.
- The callback (num) => num % 2 === 0 filters out elements not divisible by 2.
- The resulting array contains only even numbers.
- reduce The reduce function accumulates array elements into a single value based on a callback function.
- forEach The forEach function executes a provided function once for each array element.
- find The find function returns the first element in the array that satisfies a given condition.
- some The some function checks if at least one array element satisfies a condition.
- every The every function checks if all array elements satisfy a condition.








Top comments (1)
A higher order function can also do both of those things - it's not one or the other.