Higher-Order Functions: The Basics
Higher-order functions are very important and useful in the world of programming. Imagine each function as a small building block. Higher-order functions allow us to put those blocks together to create more powerful functions and cut down repetitive programming and logic. A higher-order function is defined as a function that can either take in one or more functions as arguments or it can also return a function as its result. You’ve probably already been using them without even knowing. A few built-in higher-order functions are things like map, filter, reduce, and sort. These would all be considered higher-order functions by themselves.
Let’s jump into some code here just to show you a simple example of a higher-order function. First, let’s demonstrate some simple first-class functions as shown below:
Now you will notice that what we are doing above is slightly repetitive. We are going through and defining the same type of logic and we’re setting up the expression to evaluate each time and we’re really just changing the number that we’re passing. This is a great time to use a higher-order function to simplify what we are doing and make it look a little nicer in the process.
Let’s start with creating a new function that we will call requiredAge and this will take a parameter called requiredAge and this will then return an anonymous function that will take in an age as a parameter. In that function, we will return age is greater than or equal to requiredAge. This is considered a higher-order function because it’s returning another function inside of it. Now I’m going to create a couple of variables and going to name
our first variable canDrinkAlcohol and this variable is going to equal the requiredAge function and we’re going to specify that the age for that needs to be 21. I’m going to do the same thing for canDrive and set that age at 16, as shown below:
Now that we have those defined let me just explain quickly what’s happening. First, we’re assigning variables and the return value of the requiredAge function. We need to do this because we’re calling that function and passing it an argument, so in the first case we’re passing the argument of 21 and then 16. Secondly, once we call these functions they return another function where the variable gets the value of this function. This function has not been called yet; this function is simply being returned since we know that we can do that with first-class functions in JavaScript. Once we have these variables assigned we can then call them as if they were functions. Below shows what a console.log for these variables will now produce.
So far so good? Now let’s take it one step further. I’m going to create an array and call it family and it’s simply going to be an array of names and ages. Once I’ve done that we can now loop over all these people and we can check if they can do either of the 2 types of activities.
Our results would then be:
There you have it! A brief overview of higher-order functions and how they can help you simplify your code and give you a few extra tools to make your life just a bit easier.