Array Methods
JavaScript provides several higher-order functions as part of the built-in Array
object. These functions help you manipulate and transform arrays in a functional and declarative manner. Some common array methods include map
, filter
, reduce
, forEach
, and find
.
Start creating our own
You can create your own higher-order functions by accepting other functions as arguments or returning functions as results. Let's see a couple of examples.
const add = (x, y) => {
return x + y;
}
const multiply = (x, y) => {
return x * y;
}
const applyOperation = (operation, x, y)=> {
return operation(x, y);
}
console.log(applyOperation(add, 2, 7)); // output: 9
console.log(applyOperation(multiply(10, 10)); // output: 100
How its working? Explain
In above example, we have created a custom higher-order function called applyOperation
that takes a function and two numbers as arguments and applies the function to the numbers: