Higher-Order Functions in JavaScript
2 min readNov 6, 2021
- In javascript functional programming is powered by
Higher-Order Functions
.

- A function that takes another function as its argument or returns a function is called a higher-order function
- Let's assume, we are given an array of radius and we have to find its area, circumference, and diameter.
General implementation

- The above code is absolutely correct, it satisfies our requirements.
- But, there is a problem with it. We are repeating ourselves a lot. Other than the core logic there is no difference in the code.
- It violates the concept of DRY ( Don’t repeat yourself)
Higher-order implementation

- Now our code looks much cleaner and it becomes easy to add custom logic in the future.
- Here the function
calculate
takes a callbacklogic
function to perform the intended action.
JavaScript inbuilt functional support
- If you look into the above code, the
calculate
function is pretty much doing the same thing whatArray.map
does. - You could see most of the JavaScript methods take a callback function, it's the core of functional programming.