+ 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... đ©
2 Answers
+ 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.
+ 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.