JavaScript Self Closing Function

Self Closing Functions

In order to limit the scope of a variable to local level or to reuse the same variable/function name, JavaScript provides an option of self closing anonymous functions.

Syntax:

(function(){

----your function here---

}());

The () after the anonymous function ensures that the code runs immediately.

The () in which the entire function is wrapped will instruct JavaScript that it can be a nameless anonymous function.

The anonymous self closing function comes in handy when you’re referencing to so many other functions or libraries in your code and that you don’t have to worry about name conflicts of your variables or functions with other referring functions.