In this video, we are going to create a function that retains another function.
So which means that in JavaScript functions are first class citizens because a function can retain another
function.
In the previous video, what we did was that we pass in a function as an argument to a function.
So this is what you call high order function.
Again, not all first class functions are high order functions.
Great.
So let's see how we can create a function that retains another function.
This looks weird, right?
So let's see how we are going to implement it.
We are going to write very simple function and let's see how it's going to retain another function.
So here we go.
We are going to use normal function syntax.
The name of this function is going to be add to nums, meaning add two numbers for the first one.
For the meantime, let's leave it as empty.
You are not going to pass in any argument this particular function and then my carry braces.
So here you go.
Return retains what retained a function.
But remember the function that you are going to retain.
Normally we use what is called anonymous function, meaning a function without a name.
So we are going to use normal function or we can use every function.
So let's use normal function.
This function has no name, so this is a valid syntax.
So what we have written is called a function returning another function for the inner function to we
are not going to pass in anything.
So for the meantime, let's log some text to the console and says that.
Code.
Perfect.
Now we have written a valid function that returns another function.
So let's check our console if something went wrong.
Indeed, there is nothing wrong.
This results is from this function.
So let me comment this one out.
Let's see how we can call this particular function before we call it.
Let's see how it's going to be called.
Great.
Assuming that we don't have this in a function over here, then this function is going to return a value,
whether console.log or with any logic.
So the first step is we can assign the result.
Let me say E to the first.
Let me say first function.
First function is equal to my add to numbers and I'll call it as a function.
Good.
So it means that what comes out from this function is not a value, but instead is this particular function.
So behind the scene this one is also a function.
So if you console.log first function, I'm going to see that it's going to be a function.
So let's just drop or dump it into the console and let's see now indeed we have seen what is called
a function down here.
So if this a function, then it means we can invoke it, right?
Or we can also check using type of operator if indeed it's a function.
Now, if I save it, you can see that, yes, this is a function because the first function is returning
the inner function.
So what I got back after calling this function, it's a function.
Great.
So now that we know that is a function, we can go ahead and call this function.
So if I call this function down here now let's see.
Now we see that I got called.
Now the function has been called.
So let's move step by step here.
Great.
So the same thing.
I can also assign a value to this function that I'm going to invoke.
So counts, let's say second.
Second function is equal to.
My first function as that.
So now if I console.log, she can't function.
Let's see what we have.
Indeed.
You see, I got code.
Good.
We also have another way of calling this function.
So let's see the other option tool.
So for the meantime, let me comment.
This one's out.
Perfect.
And now what I'm going to do is that for the first one, let me bring this one in the mix or better.
So let's start from clean sheet.
So I'm going to call my first function as that great because a function, I can invoke it as that.
And what comes back is a function.
So I can also call it as that.
And this is perfect syntax.
If I save it, you can see, let's see when I save this particular function.
Let's see.
You see, I got caught.
So looking at this entire.
Which one do you think is okay or is clean based on your preferences?
The choice is yours group.
I think this is very easy, but if anyone to JavaScript this one looks great.
Right.
But let's stick to this for the meantime.
Good.
So now see that the first function that you create, it doesn't take in any agreement.
So now let's do some logic here.
So let me maintain this one.
They maintain this one.
So let me copy this one and then copy.
Copy this one and comment.
Now come down here.
Let me pass it.
So let me also remove this one.
Now let's add some agreement and let's see how we are going to call this particular function.
So for my outer function, I'm going to take in a value of a as an argument.
And the second function, that's my inner function, take a value as B, so for this one, it's going
to return A plus B So this is a valid syntax.
So let's see, I'm going to call this function the same way as we called the function without argument.
So coming down, here comes let's see, first function is equal to the add to numbers function.
Now remember you see that it takes in an argument a it can be any with a string or a number or whatever,
so you pass in as any and then it's going to retain an anonymous function as that.
So for this one, I'll pass LC to now what comes back when I call this function, it's a function.
So it means that this also a function, right?
So I can assign a variable to this particular function, then I'll pass in my argument.
So this one now represents my inner function.
This one represents the outer function or the main function.
And what comes back is this function which represents this particular function down here.
So here we go.
I can also declare a variable and say second function is equal to my first function.
Then I'll pass in, let's say three, meaning I'm going to add two plus three together.
So let's see the results if I console.log.
The second function.
Archiving changes one to, let's say, resort or answer, because what we get back from this in a function
is a value but not a function.
So I can rename this one to resort and as that and this one is also perfect.
So we can also call the function as we did here, using this syntax.
So here we go.
So let me comment this one and now let me come down here.
So I'm going to use, let's say, add two numbers.
The first function are parsing as to my second function of Python three.
Now, what comes back from this function call is my result when I make use of result.
Now, there you go.
If I console.log results.
Now let's see.
It's going to be the same as five as we did or as we saw from this function call.
So this is how we can return a function from a function.
And if we have to go deeper level since that.
Well, I also want to call this function.
Maybe I also want to return a function from this so I can do let's see for this one to I can use a function
like that.
And here we go.
Then I can return.
I'll pass this one in a C and then here as A plus B, plus C.
And now here we go.
So to call this function or add additional function call or pass n, as for when I save it, well,
I get ten y time equals two.
Plus three is five plus four is nine.
So y ten.
Oh sorry I added a which is two plus B plus B two times which is six.
That is why I got that.
So here are going to be as C.
Yeah.
Sorry, I get c sorry here.
Supposed to be C.
And indeed it's not.
For the meantime, let's just retain only one function.
Going to be A plus B for now.
So here I remove this function core.
So can you rewrite this one using a row function?
Here we go.
So here comes.
I'm going to make use of every function.
Add two nums non equal to that.
And here.
Here we go as that.
Now, my inner function is going to return a function as that.
I.
Now here I will add our person here, A and B and I here are going to be a plus B and it's perfect.
It's perfect.
So I can rename this one to ad number two so and a half name conflict here now to call this function
is also as before I'm going to use this are two numbers as before I'll pass in three and I'll start
and I'll pass in four.
Well, this also perfect way to write.
I think B is undefined.
Where is my B or he is supposed to be b?
Yes.
Good.
So.
Well.
So take away.
Here is a function.
Can retain another function.
Well, you're not going to see the kind of syntax often, but it's a good idea to know how it works.
So in case you find it somewhere else, you know that all a function can return.
Retain a function.