IIFE Immediately Invoked Function Expressions Javascript
- WhSelf Reviewed. is IIFE?
- IIFE means Immediately Invoked Function Expressions.
(function square(num) {
console.log(num * num);
})(5); // can also pass arguments
Q. What will be the output of the following code?
(function (x) {
return (function (y) {
console.log(x); // 1
})(2);
})(1);
- You may think the answer is
undefined but the answer is 1
- This is because of Closures Javascript
- The ability of a function, to access variables & functions that are lexically out of its scope are called closures.