DEV Community

Paulo "Heres" Campedelli
Paulo "Heres" Campedelli

Posted on β€’ Edited 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)

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay