What is this (IIFE) construct in javascript?

Immediately-Invoked Function Expression, or shorter: IIFE. It executes immediately after it’s created.
This pattern is often used when trying to avoid polluting the global namespace, because all the variables used in the function are not visible outside its scope. It’s good to use when we have multiple functions with the same name.

<script>
//IIFE Immediately-Invoked Function Expression
var b = ( function(){
        function a(){return 5;}
        return a();
}());   
</script>

<script>
    function a(){return 4;}

console.log(b);
</script>

The output will be 5.

Leave a Reply

Your email address will not be published. Required fields are marked *