18

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?

0

1 Answer 1

24

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

3
  • And you end up with a closure. Commented Nov 11, 2017 at 22:12
  • 3
    JavaScript is a language in which all functions are first class. Meaning you can pass them around in variables like any other value. A closure is a function that has access to the enclosing "lexical scope" in which it was defined. Works a bit like how an objects methods have access to the objects state variables. Commented Nov 11, 2017 at 22:55
  • 3
    In this particular case he is currying 2 args down to 1. Commented Nov 13, 2017 at 2:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.