# ⛎ IIFE - Immediately Invoked Function Expressions
- Can be used to aviod lexical hoisting
(() => {
// code here
})();
# When to use it?
The most common use case for IIFE is to create a closure (opens new window) with some private variables inside of it.
// closure
var counter = (function() {
// This variable is private. You cannot change it from the outer code.
var i = 0;
return function() {
return i++;
};
})();
console.log(counter()); // 0
console.log(counter()); // 1
console.log(counter()); // 2