Will the two following different lines of code do exactly the same?
(function(){})();
(function(){}());
Jon Purdy
55.4k9 gold badges101 silver badges170 bronze badges
1 Answer 1
Yes. The only reason to include parentheses around the whole expression is to avoid its interpretation as a function declaration:
(function f(){}()); // (1) Expression
(function f(){})(); // (2) Expression
function f(){} // Function declaration
function f(){}(); // Syntax error
But whether you invoke the function literal directly (1) or have an intervening pair of parentheses (2) makes no difference at all.
answered Mar 19, 2014 at 19:28
Jon Purdy
55.4k9 gold badges101 silver badges170 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js
)();while the second one ends with());. What that difference means would depend on what language this is, which you didn't mention. :)