Strange JavaScript syntax like this: (function(){//code}) ();?

function, javascript

Solution

It's functionaly equivalent to doing something like:

var myFunc = function(){
    var b = 3;
    a += b;
};

myFunc();

It's got the parenthesis around it (and trailing) so that the function is called immediately. As others have said, the concept is called an anonymous function.

Problem

What does the below JavaScript mean? Why is the function embedded inside ()? ``` (function() { var b = 3; a += b; }) (); ```

Original source