Higher-order functions are functions that either take other functions as arguments or return functions as their result.
Why Use Higher-Order Functions?
- Code reusability and abstraction.
- More concise and readable code.
- They facilitate functional programming techniques.
Higher order function has one or more parameters as a function. In the example below, calculate
is the HOF which has function operation
as parameter. The function that is passed to an HOF is known as a callback function.
// Higher-order function that takes two numbers and a function
function calculate(a, b, operation) {
return operation(a, b);
}
// Example functions to use with applyOperation
function add(x, y) {
return x + y;
}
function multiply(x, y) {
return x * y;
}
// Using the higher-order function
const sum = calculate(5, 3, add); // sum is 8
const product = calculate(5, 3, multiply); // product is 15
console.log(sum); // Output: 8
console.log(product); // Output: 15