Higher Order Functions

Arvin Fernandez
3 min readJul 20, 2021

Most often in programming we use low-order functions. These are functions that take in and return any value, except a function. High order functions on the other hand are functions that either take in a callback function, and/or return a function definition.

Thanks to functions being first-class in JavaScript we are able to create and use higher order functions. Higher order functions are crucial in functional programming as they allow for reusability and function composition.

Most of us have even used them without knowing they are high order. Most commonly when using libraries or even methods like map or filter which take a callback as an argument.

Take filter for example. It takes in a function which returns a boolean value. Iterates through the array. Then pushes the current element to a new array if the callback evaluates to true when passed the current value.

let animals = ['dog', 'cat', 'horse', 'cow', 'cat']let myCatz = animals.filter((animal) => animal === 'cat') 

Nice and simple. A big plus to higher order functions is that they can make functions polymorphic! In this example we are dealing with simple strings but we can easily filter out numbers

let nums = [1, 2, 3, 3, 4, 3, 5]let threes = nums.filter(num => num === 3)

objects

let people = [{firstName:"John", lastName:"Doe", age:46},
{firstName:"Michael", lastName:"Scott", age:50},
{firstName:"Jane", lastName:"Doe", age:46}]
let theDoes = people.filter(person => person.lastName === 'Doe')

and any other value that can be stored in an array.

Like I mentioned earlier, higher order functions can also return functions. Currying and partial application techniques are perfect examples of this as they reduce the number of arguments passed to a function by returning a function.

Say we wanted to create a function that multiplies two numbers.

let multiply = (a, b) => a * bmultiply(2, 4) // => 8 

Let’s say that now we need a function that just simply doubles the number passed in.

let doubler = (num) => num * 2

And now we need a function that triples the number passed in. Instead of going ahead and creating a new function, we can just rewrite our old multiply() using the currying technique.

let multiply = (a) => (b) => a * b// its a lil confusing with the arrow syntax
// but this is the same as writing
function multiply(a){
return function(b){
return a * b
}
}

Now we can reuse our multiply function to create new triple and double functions by using partial application and higher order functions.

let doubler = (num) => multiply(2)(num)let tripler = (num) => multiply(3)(num)

Just to be clear, this is considered a higher order function because it’s returning a function. Which can then be called by just passing in the first argument because of closures.

Conclusion

Although JavaScript is a OOP language, it relies heavily on functions. By treating functions as first-class citizens it gives the versatility to choose a different pattern such as functional programming which relies mostly on the use of smaller functions to create much more complicated ones. Both paradigms have their pros and cons but its up to you to find out which one you would rather work with.

--

--

Arvin Fernandez

Junior Developer trying to help other developers(and myself) better understand programming.