How you can declare a function in JavaScript?

Let’s go straight away into the demo part So now let’s see first how we can declare a function right So we write the function keyword on than the name of the function Let’s call it parent right So this pain and function will do nothing but it will console it Will print control lot log Andi Let’s bring some strings Let’s say hello right That’s it Now this is how we create a function right on Let’s me copy Paste it in here in the console Right So now when we want to call this function we will simply do this thing.

This is simple We know this thing right We have already covered conscience right now What if there is another function that is inside this function Let’s call that function a child, okay And it’s also doing some brain Alright lets a brain clog by right and then what this function does the parent function it will return this function that is child function without in booking it Right So once it has returned this function right.

Now let me copy-paste this thing again in a console Right So now once we have created it now once we will call this parent right You see this function was never caught right Now if we want to call explicitly this function so what do we do How would we call this function if we want to call this Uh now what if we want to call whatever is in this child right So we want to call that function because it printed out the statement Then it calls that function right It didn’t call that function So to call that function we will simply right What parent clearing Then it will return a function Right So this function gets called it will return a function Then we’re calling that function right which it returned Right So it prints out hello and bye.

This is one way of how we can write nested functions right So basically it’s a function returning a function now so what is a closure So basically ah closure is when a function isn’t self-contained So right now if you see this function is self-contained because it is not dependent or no variable is defined in the parent function that we’re using in this child function So this is not a closure, in short, Okay Now how we can let me just clear the screen on the other side Now if you want to create the same function on we want to use closures So what we will do well just simply create a variable here.
Let’s suppose we’re X on That is equal to 10 No what this inner function does it will use this variable x l x Suppose we’re adding tend to it, Okay Now let me just copy this thing again okay Now what happens here This inner function isn’t self-contained So here now robot demean myself Contained again Same thing The outer The inter function is using some variable which is defining the parent function Right The outer function So this is what is closure in the most naive simple right So now if you want to call this function we will again use parent on Then again we use to parenthesis And that’s 20 so 10 plus 10 right So this is what the closure.

Basically Now let’s see those examples which were shown on your screen right Let’s see those examples and then we will die on them Okay so basically if you see here we’re using a function got named family on The argument has been passed as grandfather right G father on it returns another function which is arguing Father Right And that function is returning another function which is having an argument as son and then in that function In that function we’re using the variables which were defined in the outer functions Right So Father which was just defined about son is the parameter That argument is variable has been used in self The grandfather has been used in sun so this is a complicated example A little bit complicated example off What is a closure Is it clear Right So now let’s see a demo off it.

We’ll just open this browser as well on here and we’ll type this thing Okay so now let’s clear this thing Okay so now let’s right Let’s see So basically what we’re doing here we’re creating a function right function That’s the name it’s named family, Hey Or let’s name it to function if tonight So now this is having a Jeff right The grandfather And then this function is returning Another function right Then this grandfather is returning Another function that is What was that So this function gets called on Now this In this function we have another argument that is father right.

This function as returning another function That is what that is Dysfunction And then this function is having another argument that is son So we know if we want to print with the help of Little’s right the dollar sign We know we have already covered that how to print without using the concatenation operation and all those things we will simply use what On the sign that is below your escape he just shift Just put this these two signs that are the commas right on Then what we use We use a dollar right a dollar sign on then the name of our variable That is GF that his grandfather on without congratulating it.

We put some kind off right Some kind of separator That is this sign Then we will use again a dollar sign right then the father then the and this thing then dollar on then the sun Right That is this right So this what it does it is This function that I’m highlighting is dependent on the arguments or variables which were defined in the above function that is fright Father has been used Grandfather has been used and the sun of this particular function right This have a jam highlighting So now this is okay So we’re missing out the parenthesis so let’s put But And this is here right.

We have 123 opening entry closing So let’s copy-paste this thing and put it in our console unless present Okay so now how we can call this function Basically we use fone and then we will pass the argument Let’s suppose who is your grandfather Let’s suppose a is my grandfather then B B b is my father on Then C C c is my son Okay So now what should be output It should be a then to separator That is the hyphen sign then hyphen then B B B then C C c, Okay It is not defined Okay Okay These are strings right.

We’re using them as a string so we have to pass in them Right So these arguments are treated as a string so we have to put these sites, Okay So now if you see that a baby B and C C this is a complicated a little bit complicated example off What is closure now If you want we can just continue with the other example right Which was shown that we showed earlier with using We’re now we will be using an arrow function to define the same thing right now If you see at a function as much easier to write though you need to know arrow functions We have already covered those arrow functions.

We are It is another type off What function Expression Right Right So let’s do it with the help off function Our Carol function Right So now let’s go back So now let’s do it with the help of arrow function let’s remove this thing, Okay Now let’s see how easier it is right It is to write with the help of pharaoh functions So you define the let right So let me a name that function Fone which was which we use earlier right And then the argument Right Jeff And then we’ll use this arrow sign.

Leave a Comment