Higher Order Function and Callback in Javascript

karatasebu
2 min readNov 25, 2021

Welcome to the JavaScript’s fancy functional programming world. In this article I will try to explain what are higher order function and callback and how we can use them.

If you have been programming in JavaScript for a while, you may have already used them without even knowing. Before coming to main topic I just want to mention what is first-class function. In JavaScript functions are first-class functions. This means that functions are just like other variables (string, number, object). They can be stored as variables, used in arrays, assigned to objects, passed as a argument and returned from another functions.

Now we can understand higher order function and callback better. Higher order function is a function that receives a function as an argument or returns the function as output. (filter, reduce, map) are some of the built-in higher order functions in JavaScript. They can take callbacks as argument. Oh wait. What I just said? Callbacks? Yes, you are right, callback is a function passed as an argument to another function. Ok, I know you don’t understand 100%. Let’s jump into example, learn how we should use and make it more clearer.

Imagine we have a function and it’s task is to take an array as argument and multiply by 2 like below.

We have another function to divide. Let’s define new function called divideBy2.

But what if we want to change some functionality, in this case we have to apply changes to both functions. Think there is more functions and more complicated functions, this would be so hard. But what would happen if we used higher order function and callback.

manipulate function is higher order function and operate function is callback. Now we have more readable and maintainable code. That’s all for this article, see you in others.

Happy coding.

--

--