How to properly end a javascript statement?

How does enclosing a Javascript function invocation in parentheses affect the semantics?

  • Why is (function(){...}()); equivalent to (function(){...})(); when function(){...}(); is an error? Looking at the "railway diagrams" in "The Good Parts", even the last of these appears valid: a statement can be an expression; an expression can be an expression followed by an invocation; an expression can be a literal; the last line is a function literal followed by an invocation.  Yet there is clearly something in the language definition that says otherwise. This is a follow-up to (this is what I thought that Q was asking, hence my dumb, now deleted comment).

  • Answer:

    wrote a very thorough article regarding Immediate Invoked Functions Expressions, which can be found here: http://benalman.com/news/2010/11/immediately-invoked-function-expression/

Rick Waldron at Quora Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.