Self-invoking functions in Javascript

IIFE — IMMEDIATELY INVOKED FUNCTION EXPRESSION

Photo by Roman Synkevych on Unsplash
function myFunctionName (parameters) {
//some code that does something.
}
myFunctionName ("Function parameters");
(function(){
console.log("This function is called immediately");
})();

How they actually work ?

How to write them ?

(function () {
// body
}());
(function () {
// body
})();
(function (w, d, $) {
// body
}(window, document, jQuery));
(function(window){
var pi = 3.141;
function e() {
return Math.E;
}

window.pi = pi;
window.e = e;
})(window);
> pi; // 3.141
> e(); // 2.718281828459045

Important note:

Conclusion

Thank you for your time. Any comments and feedback are most welcomed 😊

Front end developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store