14

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?

20

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

  • And you end up with a closure. – Thomas Junk Nov 11 '17 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. – candied_orange Nov 11 '17 at 22:55
  • 3
    In this particular case he is currying 2 args down to 1. – user949300 Nov 13 '17 at 2:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.