‫So up to this point, we've been using named functions, which is definitely the most common type.
‫However, we also have the option to use anonymous functions, also called lambda functions.
‫And there's a few different types of anonymous functions, such as closures and callbacks.
‫And I'm going to give you some really simple examples of these.
‫So anonymous functions are created using the function keyword.
‫So let's go ahead and add that just like we would with a named function.
‫Except we're not going to say you know, whatever function name is here we're not going to do that.
‫We're just going to have our, our parentheses and then our function body.
‫And let's say that this takes in let's say a number okay.
‫And then in here what we'll do is we'll, we'll just make this square up a number.
‫So we'll take, we'll return whatever that number is multiplied by itself okay.
‫So that's an anonymous function.
‫And notice I have a squiggly red line here.
‫That's because with an anonymous function we actually need to put a semicolon.
‫We don't have to do that with a named function.
‫And you might be asking well what do we do with this.
‫How do we call this.
‫Because we're just defining it right.
‫If I reload the page, nothing happens.
‫So there's a couple things we can do.
‫We can put it into a variable and we can call it that way, or we can pass it into another function
‫as a callback.
‫But I'm going to get into callbacks in the next video.
‫So let's go ahead and put this into a variable.
‫So I'm still in JavaScript land here.
‫So let's call this square.
‫And let's set that to that function.
‫So now I could create a variable called result set that to square.
‫And if I pass in five here and then we'll go ahead and just echo out.
‫We'll say the square.
‫I will say square of five is and then we'll add the result.
‫And if we save that reload we get the square of five is 25.
‫And of course I didn't.
‫I don't have to create this result variable.
‫I could have just put this right here.
‫So that's just a very basic anonymous function set to a variable.
‫So now I want to talk a little bit about closures.
‫And closures can get really complicated.
‫So we're going to just look we're just going to scratch the surface here.
‫So remember when we talked about scope we have the global scope which is what we're in right here.
‫And then when we're inside a function we have that function's local scope.
‫And we couldn't access the outer scope variables directly in a function unless we defined it as global.
‫So one thing I didn't show you in that lesson is that we can have functions defined within functions,
‫and they each have their own scope.
‫So it's not just the global scope.
‫And then the local scope of one function, you can have multiple levels of functions.
‫And you might need to access variables from the outer scope.
‫And that's where closures come in.
‫And you can do that with an anonymous function along with the use keyword.
‫So let me give you an example here I'm going to create a function called create counter is just a regular
‫named function right.
‫So in here let's create a variable called count.
‫And we'll set that to zero.
‫Now this variable belongs to this function's local scope.
‫Now I can create another function within this function.
‫So I'm going to create an anonymous function and assign it to a variable called counter.
‫So we'll say equals function and then parentheses no name.
‫And then what I want to do is increment this this variable this count variable from within this function.
‫So we can say return plus plus count.
‫So that will increase it or increment it.
‫And we do need a semicolon here because this is an anonymous function.
‫And then I just want to return from create counter.
‫That counter that anonymous function.
‫Now I'm getting this red squiggly line and it says that I can't access that count because that's that's
‫in the outer scope.
‫Okay.
‫This right here is the body of create counter.
‫And that's the outer scope of this function.
‫So I can use a closure to access this.
‫And what we the way we can do that is simply by adding on to this use.
‫And then in parentheses we can say ampersand and then whatever the variable that we want to access.
‫So now that red squiggly line goes away.
‫And I now have access to this count variable that's in the outer scope.
‫And you can do this as many levels as you want.
‫I could have another function defined in here.
‫And we can use a closure to to get that that value in the outer scope.
‫So now what I can do is come outside of everything here outside of create counter.
‫And I'll create another variable and I can call it counter because this is something different.
‫This is inside the function that's in the local scope.
‫This is in the global scope.
‫So let's set that to the create counter function which is going to return the result of the the inner
‫function here.
‫All right.
‫So what I should be able to do is echo out.
‫Counter like that.
‫And let's just, let's add on a line break just to kind of separate things.
‫Yeah, we'll do that.
‫And if I reload, actually, I want to.
‫Let's put a line break on the end of this as well.
‫All right.
‫And you can see I get one right because it starts at zero.
‫And then this function runs and returns one for the first time.
‫Right.
‫Now I can call this again.
‫I'll just call it a few times.
‫And if I reload each time I call it, it's going to increment and return the current count.
‫So that's an example of a closure.
‫Now another reason that you would use an unnamed function or an anonymous function Lambda function.
‫There's a lot of different names for them is for a callback.
‫If you actually want to pass a function in to another function as an argument.
‫So we're going to look at that next.