0

Will the two following different lines of code do exactly the same?

(function(){})();
(function(){}());
Jon Purdy
55.4k9 gold badges101 silver badges170 bronze badges
asked Mar 19, 2014 at 18:13
3
  • 4
    Please add/tag the language you use. Commented Mar 19, 2014 at 18:16
  • The difference is that the first one ends with )(); while the second one ends with ());. What that difference means would depend on what language this is, which you didn't mention. :) Commented Mar 19, 2014 at 18:19
  • The language is JavaScript, as Jon Purdy already added. Thanks for that and also for your answer! Commented Mar 19, 2014 at 20:36

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.