Higher Order Functions

Earlier we saw examples of .map() and .filter(). Both of them take a function as an argument. They’re both higher order functions. A higher order function is a function that takes a function as an argument, or returns a function. Higher order function is in contrast to first order functions, which don’t take a function as an argument or return a function as output.

Let’s look at an example of a first-order function which filters all the 4-letter words from a list of words:

 1 const censor = words => {
 2   const filtered = [];
 3   for (let i = 0, { length } = words; i < length; i++) {
 4     const word = words[i];
 5     if (word.length !== 4) filtered.push(word);
 6   }
 7   return filtered;
 8 };
 9 
10 censor(['oops', 'gasp' ...

Get Composing Software now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.