⊗jsPmFCInr 265 of 504 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('!'); }();
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline