Now we are going to take an in depth look at functions and learn more things about them. So first of all, we know how to create functions. For example, if we want to create a simple function called multiply by two, I would just type function and then the name of the function, multi ply by two. And then I add parentheses, and then curly braces. And if there is any parameters, I should just add them here. So here I'm going to pass a variable called a parameter called number.
And what this function is going to do, it's going to multiply that number that we pass by two and return the result to us. So here I'm going to type return and I'm gonna add, I'm gonna just type number and multiply it by two Right. So this is a simple function that is going to just take a number and multiply it by two and return the result to us. However, there is another way of creating functions in JavaScript. And that way is called creating functions by some functions called anonymous functions. And as the name suggests, these anonymous functions have no name.
They are just functions with acne, but they are going to be stored into other other variables. So you are going to get that anonymous function and then store the result into a new variable. And I'm going to show you how to create anonymous functions. Just type function. And now you don't have to type a name of that function. This function has new name, just add parentheses And then curly braces, and inside those parentheses, you add your parameters.
So here I'm going to add number, number. And inside this function I wanted to do exactly as the multiply by two did, I wanted to multiply that number that we passed by to and return the result to us. So here, I'm going to copy it and paste it here. And what I'm going to do, what I need to do is that at the end, I should add a semicolon. And if now I save and run I'm going to get an error, because anonymous functions must be saved must be stored into variables. So I'm going to store all this expression all this function into a new variable called var here, new variable, I'm gonna create a new variable called var, and then multi ply valuable, valuable equals, equals if this or multiply by two variable by two variable.
So now we have this function, the anonymous function, which is going to do the same exact job as the simple function. And let's now test and see, I'm going to create the console dot log, and I'm going to call the simple function, the multiply by two. here inside the console log, multiply by two, and I'm going to pass three, and the result should be six because three multiplied by two, the result is six. Now if I save and run, what I'm going to get is six right? Now if I want to call if I want to use the anonymous function, what I would do is that I would use that variable and pass that that parameter the Number parameter to that variable and JavaScript is going to know that you are trying to pass that variable to this function to this anonymous function that you have stored it into the multiply by two variable.
So here, what I'm going to do is that I'm going to use the multiply by two variable, and I'm going to pass three as well. And if I save and run, what what I'm going to get is six, which is exactly the same as the first function. So in JavaScript, there are two ways of creating functions, either by creating a function with just a simple name, or you can get an anonymous function and store the result into just a variable. But why would we use the second version? Why would we use anonymous functions? why not just use the simple the simple version?
Well, the answer is that in many libraries, in JavaScript, in general, there are many functions that take functions as parameters. So for example, you might have a function called hear function functions or function called multi line just multiply multi ply, and it takes, it just takes a parameter, but that parameter is a function, not a simple variable. So I could just copy this function and paste it as a as a parameter, right? copy and paste it here. So this, this multiply function is going to take a parameter but this parameter is going to be is going to be a function, not a symbol variable, as you can see here, and then here, I'm going to just add a comma and add another variable, blah, blah, blah, and a third variable, a third parameter, and so on and so forth. But that our code would be just very difficult difficult to read if we just add the complete function as a parameter here.
So therefore, what we would do instead, we just create that anonymous function and store the result or store it into a variable. And we pass that variable instead of the complete the complete anonymous function. So I'm going to delete all of this, all of this function with its its implementation. And instead I'm going to pass multiply by two variable as as, as a parameter, and that parameter is valuable, right? But it's, it's actually a function, because the anonymous function is stored into this multiply by two variable, then we just take this function and use it inside the multiply function and this is commonly used Use this format is commonly used in libraries like jQuery. And even if you want to create something called a callback function, you would have to use functions as parameters.
Therefore, it's very important to know this concept that the anonymous function should be stored into a variable. And it's commonly used in JavaScript. And this is why we use anonymous functions to pass them as variables into other functions.