DEV Community

Paulo "Heres" Campedelli
Paulo "Heres" Campedelli

Posted on

High Order Function !== Closure

TL;DR

Every closure is a higher-order function, but not every higher-order function is a closure. To be a closure, the inner function has to have access to the parameter of the outer function.


Closures and higher-order functions may appear similar, but they have a key differentiation between them (someone I know didn't pass a recent interview for a summer job because he couldn't explain it!). Enough talk, let's get straight to the point:

What are Closures?!
Closures are inner functions that can access the parameters of outer functions. In the example below, the innerFunction can access and use the val parameter from the outerFunction.

function outerFunction(val) {
  return function innerFunction(anotherValue) {
    return `${val} ${anotherValue}`;
  };
}
Enter fullscreen mode Exit fullscreen mode

What are Higher-Order Functions?
Similar to closures, higher-order functions are a structure of a function, but they do not necessarily have to be closures.

function outerFunction(val) {
  return function innerFunction(anotherValue) {
    return `${anotherValue}`;
  };
}
Enter fullscreen mode Exit fullscreen mode

I hope that this may help you! 😁

Top comments (0)