We can create functions that will be called immediately upon definition. Such functions are also called Immediately Invoked Function Expression (IIFE). Such functions are enclosed in parentheses, and after the function definition, the parameters are passed in parentheses.
(function() { console.log('Its worked'); })(); ( function (n) { var result = 1; for (var i = 1; i <= n; i++) result *= i; console.log("" + n + " = " + result); } (4) );
You can also pass arguments to the self-calling function:
(function(name) { console.log('Hey, ' + name + '!'); })('John');
In this example, the function takes a name argument and is immediately called with the ‘Vasya‘ argument. The result will be the output of the line “Hello, Vasya!” to the console.