⊗jsPmFTDTCh 235 of 504 menu

How to check the type of a function in JavaScript

In the tasks below, you will need to define a function defined as a Function Declaration or Function Expression.

In simple cases, this is not difficult to do visually. But how do you check that you did it right? Use the difference between Function Declaration and Function Expression: the former can be called above their definition, while the latter cannot.

Let's say we have such a function:

let test = function() { console.log('!'); }

Let's invoke this function before it's defined:

test(); // throws an error to the console, that means it is Function Expression let test = function() { console.log('!'); }

Here is another example:

func(); // shows '!', it is Function Declaration function func() { console.log('!'); }

Let's put a plus sign in front of our function:

func(); // throws an error to the console, it is Function Expression +function func() { console.log('!'); }

Since the function above is a Function Expression and it is not assigned to any variable, there is no way to call it, because it will not be available by the name func.

Determine if the presented function is a Function Declaration or a Function Expression:

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

Determine if the presented function is a Function Declaration and a Function Expression:

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

Determine if the presented function is a Function Declaration and a Function Expression:

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

Determine if the presented function is a Function Declaration and a Function Expression:

function func() { 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