Self-calling functions in JS. Code example open

Self-calling functions in JS. Code example

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.

0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation