We've briefly discussed IIFE functions in earlier chapters. It is basically an anonymous function that is executed automatically. Let's take a look at one example. This is how a typical old JS module that uses IIFE looks:
//Module Starts (function(window){ const sum =(x, y) => x + y;const sub = (x,y) => x - y;const math = { findSum(a, b) { return sum(a, b) }, findSub(a,b) { return sub(a, b) }} window.math = math; })(window) //Module Ends console.log(math.findSum(1, 2)); //Output "3" console.log(math.findSub(1, 2)); //Output "-1"
Here, we created a module using IIFE. The sum and sub variables are global to the module, but not visible outside the module. The math variable is exported by the module ...