In this video.
Let's talk about high order functions.
Understanding this concept is going to help you to understand the code that you write.
So what is higher order functions?
Well, the previous video we talk about functions as a first class citizen.
So what is high order functions?
High honor function simply implements that functions, accepting functions as a segment and is also
called callback functions.
Again, callback function simply means that a function that is being passed in as an argument to and
a function is said to be callback functions or high order functions.
Well, in JavaScript, high order functions are everywhere.
Talking about a real matter is the old David Ward high order functions.
So let's see examples of high order functions in JavaScript.
Do you remember we talk about reduced function.
And you also talk about find method.
And for Ouattara.
And map.
All these are based on high order function because it takes in a function as an argument.
So let's say that we have this data here, let's say coms.
Let's say quantity here is equal to array of, let's say one.
Let's say one, four, five, and let's see these values.
Now, I want to loop through this and display individual record or I want to add up the values from
this and I can use reduce.
So let's make our life easier and use, let's say find.
I want to find if a value exists in this particular array.
So here we go.
I will take my quantity array and then dot find.
And here we go to see that we need to pass in a function, not a variable, a function.
And that function has no name.
That's what you call anonymous function.
Here we go.
As that you see many that this function is high order function because it's taking a function as an
argument.
So here I can go ahead and retain or console.log it.
So inside my callback here I have access to my record as Q to where let me say data so I can quickly
console.log it as that and let's see.
And indeed I have the record.
So find is said to be high order function because it's taking in a function as a callback.
So let's see how we can create our own high order function, a function that's going to take in the
function as a callback.
So let's see.
So let's remove this once and let's start with clean sheet.
Let's say that we want to calculate a bill and at same time we want a function to display the result
of the bill.
So we have two functions here.
Say, calculate, calculate bill, and then second function is going to just display the bill as that.
So here we go.
So for the first one, for calculate, let's come here and say cost calculate.
Bill and this going to be.
A function, and this function would take a the quantity I want to calculate and then the price I want
to calculate.
So here we go.
So I will return here as my quantity multiplied by the price.
Now, here we go.
So the next one, I'm going to be this play, what they build.
Because here the response or the results coming back from this one is going to be the build I want to
pay and this function is going to take the result here and then display it.
So let's see how we are going to write that function.
So it's going to be cost, let's say, display.
Display below, and this function is going to take in a function as an argument.
So here we can name it whatever you want as a function or callback or the actual name.
The choice is yours.
So let's use a name that is more descriptive.
So let's say calculate below function.
We know that this function is going to take in the calculate function.
So here let's just console log into the console, but here we go.
Okay.
Before I do that, let me show you how we can I mean, return from this function.
We can return from this function in two ways.
One, either call the function directly, call build function inside us.
Call be a function and as that did not pass in the value I want to calculate or I can remove it as that
and upon calling our pass in my agreement.
That's the right way to go.
So for this one, let's console, log it to the console and start.
Great.
So now how can you call this particular function?
Remember, I'm going to call this play bill.
Display beer, then this one would take a.
You see that callback function here.
Is it I have calculate beer function any then I'll pass in my calculate beer function.
As that.
And this function takes inward to agreement quantity and price.
So let's say quantity is two and price is five.
Let's see, now I have ten.
So how is it?