Sitemap
Benjamin Sullivan

Benjamin Sullivan

Full stack web developer using Angular, Rails, JavaScript, React, and more to develop and deliver personally interesting projects.

Higher-Order Functions in JavaScript

3 min readJul 12, 2020

JavaScript’s treatment of functions as values allows for some really interesting functionality. At their very simplest Higher-Order Functions (referred to as HOF from here on) are functions that accept other functions as arguments or return other functions as values.

Press enter or click to view image in full size

There are a few underlying technologies that allow you to create HOF. The first is that JavaScript has first-class functions. This means functions can then be treated as values. They can be can declared and initialized constants or variables like so:

const addTwoNumbers = (x, y) => x + y;

Secondly, once declared a function can then be treated as a variable. Like:

let simpleFunction = addTwoNumbers;

or in the case of HOFs:

arrayAddTwoNumbers([1, 2, 3, 4], addTwoNumbers);

In fact there is not much practical difference between variables and functions except that arguments can be passed into functions so some work can be done with them.

The final underlying technology is that functions can be returned as values from other functions like:

const multiplyByNewNumber = num => {  return function(newNum){ return newNum * num; }}

This allows for another interesting technique besides HOF, called closures that I will not be going over, but are definitely worth looking into.

HOF are pretty tightly tied into basic methods of the language. If you have used .map, .filter, or .reduce you have used a HOF. The callback function can either be defined in the arguments or it can be passed in as a variable. For example:

array.map(item => item * 2);

and

function multiplyByTwo(item){ return item * 2; }array.map(multiplyByTwo);

are both valid ways of doing the same thing. Usually the function passed into these functions are very simple, but they can very easily ramp up in complexity.

Get Benjamin Sullivan’s stories in your inbox

Join Medium for free to get updates from this writer.

Example

Let’s say you’re working a young start up and your server costs are eating away at your run way. You have a server that performs the validations, but uses some non-zero amount of compute power to do so. You want to maybe offload some of the computationally heavy validations so information sent to the server from your front end does not have to be heavily processed by your server hardware. Pushing that sanitation to the client’s machine could cut down on your server costs.

For the sake of the example, let’s say you have two types of users and different types of validations need to be run on each before being sent off the server. Since JavaScrit treats those functions as first-class values we can, and have, stored them in different arrays depending on their use case.

I’ll call the arrays that hold our validation functions userValidations and premiereUserValidations. Let’s say that a premiereUser starts with an accountBalace of 10 and must have a stronger password. Therefore the premiereUserValidations array is at least 2 functions longer

Example user:

const premiereUser1 = {  first_name: ben,  last_name: sullivan,  password: ****************,  accountType: premiere,  accountBalance: 10,  // More fields}

Given that each validation evaluates to true or false depending on if it passes the validation. For this user, before we send a POST request, we could pass our test user and the array through a function like so:

While it’s a quick and dirty example of what can be done with HOF, there are incredibly powerful ways to combine this technique with others like closures that can make your code extensible and powerful.

As always here are some resources that were helpful while researching HOF:

--

--

Benjamin Sullivan
Benjamin Sullivan

Written by Benjamin Sullivan

Full stack web developer using Angular, Rails, JavaScript, React, and more to develop and deliver personally interesting projects.