Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Why is all javascript code wrapped in a function? #1800


User avatar
0
eggersa created

I was wondering why every javascript code is wrapped in a function like this:

(function () {
   ...
}();

I have never seen this before. Maybe there is a good idea behind this to keep going with this practice?

Thank you for your answer!


1 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    That's a very common pattern, known as Immediately-Invoked Function Expression (IIFE).

    You can find many answer on the web, example: <a class="postlink" href="http://stackoverflow.com/questions/8228281/what-is-the-function-construct-in-javascript">http://stackoverflow.com/questions/8228 ... javascript</a>

    In brief, this creates a local scope and prevents accidently declaring global variables/functions.