This way of writing a function is used in several different situations. Often functions are used to ensure that the variable names do not conflict with each other (especially if the page uses more than one script).
var area = (function() {
var width = 3;
var height = 3;
return width * height;
})();
The location where you declare a variable will affect where it can be used within your code. If you declare it within a function, it can only be used within that function. This is known as the variable’s scope.
We have 2 scopes which are:
Any variable declaration inside
a function will be considered as local variables.
Any variable declaration outside
a function will be considered as local variables.
Global variables
use more memory. The browser has to remember them for as long as the web page using them. Local variables
are only remembered during the period of time that a function is being executed. It is good to note that any local variable inside a function will be lost when the function ends.