+ 1

javascript: what kind of function is this?

I had this question in my challenge and i didn‘ t recognice what kind of / type of function is this? Never seen before... 😇 (function(){ let a = 3; }) (); console.log(a); //and i dont get a result of this function... i expected at least a „3“ as output, but there was nothing... 😩

21st Feb 2020, 8:21 AM
Mylisa_beth
Mylisa_beth - avatar
2 Respuestas
+ 3
its an anonymous function, that immediatelly executed. also it will only run once, and cannot be called. let a = 3. only exist inside the scope of that function, thats why you dont get anything when call it from the outside.
21st Feb 2020, 8:34 AM
Taste
Taste - avatar
+ 2
The result was nothing because the function doesn't "return" anything. It just setup a variable "a" and nothing else. The structure of this function is made to be automatically triggered. If you write : function(){} The function is just defined but not used yet. (function(){})() The function is defined and immediately triggered.
21st Feb 2020, 8:32 AM
Geoffrey L
Geoffrey L - avatar