There is a really important concept that you need to understand when it comes to anonymous functions. And in this video tutorial, I'm going to show you this concept. So now suppose that we have these two functions. The first one is a simple function. The second one is an anonymous function. And they do the same exact thing.
Both of them take a number, multiply them by two and just print the number on the screen, they don't return anything, just they use the console dot log and print the value or the result on the screen or on the console. So here, if I call the first method, multiply by two, and a pass, two, and if I save and run, what I'm going to get is obviously four right and this call has come after defining the function right. But suppose that I call this function suppose that I call this function before the definition of the function before different Finding the function. If Now I add it here, paste here and I save and run. As you can see, I'm going to get for what how did JavaScript know that this multiply by two is a function, even though the definition of the function has come after the call of the function?
The reason is that there is some concept in JavaScript called hoisting. And what this concept is, is that at runtime, JavaScript takes all functions at the beginning. And then each call or any call comes after the definition of the function. So at runtime JavaScript will behind the scenes just take the take this function or any function at first, and then any call will come after after that here, right? But you don't see that because it happens behind the scenes. On the other hand, for anonymous functions, the case is completely different.
So for example, if I call the anonymous function here, multiply by two, I'm going to change this, this name by multiply by two variable so that we recognize that this is the anonymous function variable. And here, I'm going to call it variable. If I pass three, and if I save and write, obviously, I'm going to get six, right? That's absolutely right. Nothing wrong with that. But if I call the function if I copy this line of code and call the function before, before the definition of the anonymous function, if now I save and run, what I'm going to get is an error.
Because JavaScript does not know that this is even a function. It doesn't know that. It it doesn't know that because this is an anonymous function, and it's stored into a valid trouble. So all what JavaScript knows is that it's a variable, this is a variable, therefore, it's not going to take this variable at the beginning of the file. And then it's not going to add this line of code this this call for the function at the end as it did with the simple function. So you need to understand this very, very well, because when later when you use anonymous functions, you need to know that you add your call for the anonymous function after the declaration or after you create the inner anonymous function definition and store it into a variable.
Otherwise you will get an error. So you need to remember that the difference between say a simple function an anonymous function is that a simple function can be called anywhere. As long as you have created the function it doesn't matter where you add the code. If you are the call here if you are the call here, if you are the call here it doesn't even matter. What does matter is that you create a function, but on the other hand anonymous functions, the case is completely different. You need to add first the anonymous function with its variable and then they call this call should be should come after, after the creation of that function.