# ⛎ IIFE - Immediately Invoked Function Expressions

1 min read, 109 words iifeimmediatelyinvokedfunctionexpressionclosure
  • 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

# References

Subscribe to our Newsletter

If you like my work and think it was helpful kindly support my work


Buy Me A Coffee