What is the definition of a higher-order function?
A higher-order function is any function that takes one or more functions as arguments, which it uses to operate on some data, and/or returns a function as a result. Higher-order functions are meant to abstract some operation that is performed repeatedly. The classic example of this is map
, which takes an array and a function as arguments. map
then uses this function to transform each item in the array, returning a new array with the transformed data. Other popular examples in JavaScript are forEach
, filter
, and reduce
. A higher-order function doesn't just need to be manipulating arrays as there are many use cases for returning a function from another function. Function.prototype.bind
is one such example in JavaScript.
Map
Let's say we have an array of names that we need to transform to uppercase.
const names = ['irish', 'daisy', 'anna'];
The imperative way will be as such:
const transformNamesToUppercase = function (names) {const results = [];for (let i = 0; i < names.length; i++) {results.push(names[i].toUpperCase());}return results;};transformNamesToUppercase(names); // ['IRISH', 'DAISY', 'ANNA']
Use .map(transformerFn)
makes the code shorter and more declarative.
const transformNamesToUppercase = function (names) {return names.map((name) => name.toUpperCase());};transformNamesToUppercase(names); // ['IRISH', 'DAISY', 'ANNA']