Check out a free preview of the full The Hard Parts of Functional JavaScript course

The "Higher-Order Functions Review" Lesson is part of the full, The Hard Parts of Functional JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

Will explains that functions are first class objects, meaning, functions can be passed as inputs of functions, and describes the difference between callback functions and higher-order functions.

Preview
Close

Transcript from the "Higher-Order Functions Review" Lesson

[00:00:00]
>> Will Sentance: How's is this possible? Well, in JavaScript, functions are what's called first-class objects, that means that look.
>> Will Sentance: Multiply by 2 here, people really, it's just an object, and I know-
>> Will Sentance: for our online audience. I know that if I were to create an object like that, that's horrible, never mind.

[00:00:33]
If I create an object like that, I can pass that into my function, no problem. In fact, arrays or objects behind the scenes, we'll see that a little bit later on. Is quite useful for us, not very functional, but quite useful for us. Based on the prototypal nature of JavaScript, it's gonna have some great features.

[00:00:50]
But to arrays or objects, functions are just objects, that means they can be treated as fully first-class. Have all the features of objects, including being able to be passed as inputs into other functions. That's exactly what we did here. That enables our functions to become reusable with different specifics of what they're gonna do.

[00:01:13]
Our copy array and manipulate can work on any specific code and what it's gonna do to each element of the array. One is going to manipulate an element of the array to do. And it means functions can be returned out of other functions, and that we're gonna see this afternoon.

[00:01:30]
Enables us to do some really profound things like function decoration, currying, partial application. All to come this afternoon, but first, let's just look at it for a second. Which of these is our callback function, which is our higher-order function? Virginia, which you think here is our grand higher function and which is our little sort of callback function?

[00:01:54]
This most profound of terms, higher-order functions, intimidating, serious, sophisticated, and we've seen it all already.
>> Regina: Would it be CopyArrayAndManipulate?
>> Will Sentance: Is our?
>> Regina: What?
>> Will Sentance: CopyArrayAndManipulate is our?
>> Regina: Higher-order function-
>> Will Sentance: It's our higher-order function, and which is our little baby callback function?
>> Regina: Multiply by 2.

[00:02:16]
>> Will Sentance: Multiply by two. Our higher-order function, folks, there it is, the function we pass and multiply by 2 is our callback function. The outer function that takes in the function is our higher-order function. It is a core component of functional programming, and that's what it is. A function that we can leave a little blank on in order that when we run it, we specify exactly what it's gonna do.

[00:02:39]
Just like we specify exactly what array it's gonna run on, we specify exactly what code it's gonna use, and that's it. All right, people, just a name, you don't have to declare them as higher-order functions. You can imagine the language where you have to say this is not a regular function.

[00:02:54]
This is one of a higher order, they can take others just by using it, by inserting a function. And so we're inserting code that then ends up being executed inside, that makes our higher-order function. Also, by the way, just as a mention, we'll see this later on. Any function that also returns out a function, we'll see that later on.

[00:03:12]
By the way, we'll see that later on, and I will say this, I give talks at these fancy developer places. And even their engineers with five, ten years experience, struggle with that notion. Of birthing a function inside the running of another function, saving it, defining it and returning it out.

[00:03:30]
They struggle with it, so we'll see that, but that's also known as a higher-order function. So let's review higher-order functions. Are the easier, do they make our lives easier to add features? Let's see what it says. We don't need to build a brand new copy array and add 3, copy array and divide by 2, copy and function.

[00:03:59]
Just use copy and manipulate and with the input of three higher-order functions are crucial for keeping our code dry. Don't repeat yourself, write it once, and then combine it up again to build new features. We turn copy array and divide by 2, copy array and multiply by 2, copy array and add 3.

[00:04:17]
Three separate megablocks of code into one, and then add the little free bit as a single line. I just inserted in, add the multiply by 2 in a single Iine, inserted it. More readable? Yeah, that's pretty readable, copy array and manipulate, multiply by 2. Once you get used to it, I understand and create a new array doing something to it.

[00:04:36]
And I'm using a multiply by 2. I know what this is doing more readily than the four loop style. Easier to debug? Well, I would argue as long as we understand what it's doing under the hood.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now