JavaScript Design Patterns – Name for Function Returning Function Pattern

design-patternsjavascript

I use this pattern quite often in JavaScript. Here is an example:

const comments = [
  { text: 'Hello', id: 1 },
  { text: 'World', id: 4 },
];

const byId = id => element => element.id === id;

const comment = comments.find(byId(1));

Sometimes, this pattern can make our code readable and modular. What is this pattern called?

Best Answer

They are called higher-order functions.

A higher-order function is a function that can take another function as an argument, or that returns a function as a result. - Higher-Order Functions in JavaScript by M. David Green

Related Topic