Check out a free preview of the full JavaScript: The Hard Parts course:
The "High Order Functions" Lesson is part of the full, JavaScript: The Hard Parts course featured in this preview video. Here's what you'd learn in this lesson:

With a brief exercise, Will reviews high order functions by examining the programming concept of Don't Repeat Yourself (DRY), which is a principle of software development aimed at reducing code repetition by making the code more re-usable.

Get Unlimited Access Now

Transcript from the "High Order Functions" Lesson

[00:00:00]
>> Will Sentance: So, before we get into how high order functions work, we are going to see a metaphor, a metaphor for why we have a high order function in the first place. So, function 10 squared, no input, returns 10*10, sounds like a very useful function, I am sure you can already agree.

[00:00:18] What is the syntax of this function, Kara? What is the wording of this function? It's a function 10 squared, no input and returns 10 by 10, how do we declare the function? What's the first word for to declare a function, Kara?
>> Kara: Function.
>> Will Sentance: Function great there it is function, and what could we call it, Kara?

[00:00:40]
>> Clara: 10squared.
>> Will Sentance: Nice Clara. Ambiguous name, 10 squared, and does it take any parameters? Cray, any parameters for it? No, correct. And then in the body of the function, what do we add? Whatever returned. 10 by 10, such a useful function. Do you agree? No, it's a terribly useless function.

[00:01:08]
>> Group: [LAUGH]
>> Will Sentance: This is a useless function, come on. It's a useless function. 10 squared evaluates to 100, perfect. Now, what if though, here's a more useful function, 9 squared. Griffin, how would I make 9 squared?
>> Griffin: Same thing, you just change the inputs.
>> Will Sentance: Yeah, what about 100 squared, Lindsey?

[00:01:30]
>> Lindsey: Same thing, you just change the numbers.
>> Will Sentance: Yeah, I don't want to really drag this out. Barb, what about 2,000 squared?
>> Group: [LAUGH]
>> Barb: Same thing.
>> Will Sentance: How much longer can I drag this out? We could go for a while. We're breaking some sort of principle here.

[00:01:43] What principle are we breaking as I cover all these functions? What principle are we breaking everyone?
>> Group: Don't repeat yourself.
>> Will Sentance: Don't repeat yourself. As developers the reason we have functions in the first place, is to wrap up some functionality that we wanna reuse and then we can refer to it just by it's label.

[00:01:59] And only write out the functionality once. So we're constantly try to avoid repeating ourselves, not because we're lazy but also because it makes for far more readable code, far more reusable code. We don't wanna be rewriting code because it makes it hard, well, this functionality is the same as here, but I've gotta change it in both places, this is problematic.

[00:02:22] So instead I write, what could I do here? Right now, 9 squared, 9 squared. We've got a problem. We're breaking this dry principle. What could be do instead here, Griffin.
>> Griffin: You could do function n squared for input of n.
>> Will Sentance: So tell me more abstractly what am I, [LAUGH] here we go.

[00:02:43] No, nobody get that pun, all right. Tell me more abstractly, abstractly, no. Tell me more. What are we doing here in a sense?
>> Griffin: Would you be I guess modulizing it and being able-
>> Will Sentance: I like that, you're making your code more resusable.
>> Griffin: Yeah you're able to call that function any time you want a number squared.

[00:03:05]
>> Will Sentance: Very nice, that's exactly right, we can generalize the function. We can create a function. It's gonna be so yeah, yeah, obviously we're not gonna create 10 squared, 100 squared. No, this is the very essence of why we have high order functions. We're gonna see in a second here we've created a function squareNum, and left a place holder.

[00:03:25] You see every now and then I realize I should be pointing up here because everyone online just like well yeah to the edge of my computer. There it is. Here, squareNum function. I left a placeholder and then it's only when I invoke call, execute, run that squareNum function with 10, 9, 100, I get to it dynamically.

[00:03:48] At the same time, create the function effectively 10 squared, 9 squared. And then call it at the same time. So at my definition stage of the function. So functions have 2 stages, definition and execution. And my definition stage, where I stored a memory, I leave a placeholder. Like a kinda I don't know yet what data I'm gonna apply this to yet.

[00:04:07] So just leave it blank for now. And I'll figure out when I run the function, what actual specific number, my data, I'm gonna apply this functionality to. Okay, this is making our function cleaner and more efficient. But this is obvious. It turns out well its obvious but it's fairly obvious.

[00:04:27] It turns out, we may not only want to leave some of our data general until we call the function. In other words leave a placeholder for some of our data, in the definition and only when we run the function determine what data I wanna apply my multiply by itself functionality to.

[00:04:51] It turns out I might also want to leave a placeholder. I might want to leave for now unknown, some of my specific functionality until I call my function. I may not just wanna say, leave a placeholder for my data, I may also wanna leave a placeholder to be determined later, for some of my actual specific functionality, and then only determine what that functionality is, when I actually go ahead and call, run the function.

[00:05:25] Here I determine my data, 10,9,8 when I run the function. What if I were to determine some of my functionality, only when I run the function. That's very powerful and that is what we're gonna see is the essence of higher order functions.