In this video.
Let's talk about high order function and function as a first class citizen.
So what is a difference is that is what we are going to talk about in this particular video.
So let's start with function as first class citizen.
What does it mean?
A function is a first class citizen.
If it can be used like any other value in the language, well, it means that if a function can be passed
as an argument to other functions, then it's being regarded as a first class citizen.
That is one point.
And if it can be retained by other function, meaning a function that can be retained by other functions.
In that case, we can regard that one as a first class citizen.
And if we can assign the function to a variable, then it can be regarded as a first class citizen.
And again, if we can store a function inside an array, then it can be regarded as first class citizen.
And in JavaScript, all functions can behave in this way.
So that's what we call.
Functions as first class citizen.
So what about high order functions?
Well, a high order function is a function that takes one or more functions as a segment or retain a
function.
Well, so it means that in JavaScript, all functions are first class citizens.
But not all functions are high order functions.
So let's have a look at example with code.
So when you say functions retaining a function, it can be regarded as what?
First class citizen.
Right.
Or high order function because it takes in another function.
So let's see.
A nice command.
Say function functions returning.
Function.
Is it possible?
Yes, it is.
So let's see.
Example of a function that's going to retain another function.
So let's say I have a function that's going to add two numbers together, two nums.
For the first function or the outer function, it will not take in any agreement, but instead of returning
a value, this function is going to retain a function as that, and this function will pass in A and
B, because I want to add two numbers together and here we go.
So now this is what we call anonymous function.
Because it has no name.
So a function without a name is regarded as an anonymous function.
So now inside my inner function, I can go ahead and retain a plus P as that.
So let's see how can we call this particular function?
So come down here and let's say invoke.
Well, let's deduce it when I call this function call.
Add two numbers as that.
Let's see, what is the response coming back from this?
Is it going to retain a value or a function is going to retain a function.
So let's assign to a variable, say answer.
If I console.log answer what would be the result?
Is it going to be the answer?
Let's say A plus B or a function.
Have a look.
If I console.log, answer now and see that I get a function.
Because it's a function.
I cannot invoke this one using parentheses as that.
Then I'll pass in my argument two and two, which is full if I save it.
Now you see that I have four a function returning a function that is one example.
And for this one we can call this function also in another way, let's say another way.
Of calling.
This function.
Have a look.
Okay, let's see.
So, first of all, I'll call my my other function as that.
And this function is not taking in anything.
Right.
No argument.
But a second function is that now pass in to comma two as let me comment this one out.
Now if I save it, nothing being displayed because there is pounds coming back from this one.
We need to save it into a variable.
So counts, let's say answer is equal to that.
If I console.log the answer, I'm going to get four as before.
So it is how we can also call the function great.
The next attribute of first class citizen is we can assign a function to a variable.
Let's see.
So now we know that we cannot return a function from a function.
So this is what you call first class citizen.
Now, next, I'm going to be assigning a function to a variable, let's say assign function to a variable.
Let's see.
Great.
So let's say I have a function, and this function is not taking in any agreement.
Just display.
Say, how are you?
So let's see how we can assign a function to a variable.
So let's declare a variable.
Say, answer our results.
Now is equal to say hello.
Because I'm assigning a function to a variable.
I don't need to call it as this.
Right.
So I remove my parentheses.
Now I have assigned my function to a variable called result.
So if I call results.
The results.
Let's see if we're going to get the response.
Indeed we have.
How are you?
And if I call, say hello to.
We also have.
How are you?
You can see we can assign a function to a variable.
So this what you call first class citizen?
Well, we can also save or store a function inside an array.
So let's see.
Great.
So assign or store functions in an array.
Let's say store function.
E I read.
Is that so let's create two functions and let's see how we can save them inside an array or a store
or save function in an array.
So let's say I have a function and this one says, say hello.
And then console.log.
Indeed.
Hello.
And then second function is going to say hi, say hi here, and then just console.log here as high grade.
So for this one, we can save them or store them inside an array.
Let's see.
So const my array equal to that.
Then I will take my first function, say hello comma and then say hi as that.
So how can I call this function?
Remember, I can call it using the indexes.
So array, first index which is hello, this one.
When I save it, you see, we don't see the result here because I have no call the function so we need
to call it as that.
And now we see.
I see.
Hello.
I can also take the second index and then also call it as that is going to be as high.
That's why we is that functions are first class citizens.
Then this video let's talk about high or the functions with example.