Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

IIFE's and Function expressions

The way have seen for declaring functions is not the only one JS provides. The method we have used so far was global function declarations, but functions can also be declared as expressions. That means we can store a function into a variable as any other JS data type. 

Be careful! We don't store the result or what the function returns!

We have already done this multiple times. We store the body of the function, the whole functionality in other words. As you can see in line 1 we have defined a function expression. A function stored in a variable that is called greetingsJs. Now this variable holds this function as value. Thus we can call this function by the name of it's variable.

The main difference is that a function that has been instantiated as function expression can be called ONLY  after it has been defined (same as other languages like python). If you define a function with the way we have been doing so far you can use it also before. This is possible due to a JS procedure that is called hoisting. Hoisting is beyond the scope of this tutorial but if you want to learn more, read here.

Another way of defining JS functions you can see it on line 5. What is this weird syntax? This is an immediately invoked function expression. That means, this function will be executed exactly after it is defined! Not only that but it will run only once! So it can't be called further afterwards.