⊗jsPmFCInr 263 of 502 menu

Immediately invoked function call in JavaScript

Now we will analyze a trick that allows you to call a function right at the place of its declaration. This construct is called Immediately Invoked Function Expression (IIFE).

Let's look at an example. Let's say we have the following functional expression:

let func = function() { console.log('!'); }; func(); // shows '!'

Let's now not assign our function to a variable, but call it immediately, "in-place". To do this, put parentheses after the function:

+function() { console.log('!'); // shows '!' }();

The presence of a plus in this case is a required condition, since without it the function will become a Function Declaration, and they cannot be called in-place (and even without a name). Of course, instead of a plus, anything can be - the main thing is that our function is a functional expression.

Determine what will be output to the console without running the code:

!function() { console.log('!'); }();

Determine what will be output to the console without running the code:

function() { console.log('!'); }();
enru