Difference between (function(){})(); and function(){}(); [duplicate]

Solution 1:

Peter Michaux discusses the difference in An Important Pair of Parens.

Basically the parentheses are a convention to denote that an immediately invoked function expression is following, not a plain function. Especially if the function body is lengthy, this reduces surprises,

Solution 2:

The extra set of parentheses makes it clearer that you are constructing a function and then calling it. It's a coding style thing, not a functionality thing.

Solution 3:

function(){}();

doesn't work in most of browsers. You should use parenthesis around the function in order to execute it

(function(){})();

then browser will know that last parenthesis should be applied to all the expression

function(){}

UPD: If you don't use parenthesis, the brower could misunderstand you. If you just call the function and dismiss the result

function() {
    alert(1);
}();

then ff and ie will throw error