0

Both of them works without errors, is there any difference?

(function(){}());

(function (){})();

here are some working examples:

    console.log(
            (function (a) {return a*2;}(3))
    );

    console.log(
            (function (a) {return a*2;})(3)
    );
  • 1
    both are the same. just coder preference what you choose. – Bhojendra Rauniyar Jul 09 '15 at 10:55
  • No differences, you may also do that aswell: `~function(a){ return a*2; }(3);` these all are auto-executing anynomous functions, you may find even other ways they are written according to the programmer or, occasionally, according to the minifier the programmer used. You may want to either check james's link or this for more cases: https://sarfraznawaz.wordpress.com/2012/01/26/javascript-self-invoking-functions/ – briosheje Jul 09 '15 at 11:05

1 Answers1

0

I guess both are same, only thing is one is wrapped inside parantheses () and the other is not.

(function(){}());

And this is creating and executing it immediately:

(function (){})();

Example for the second case would be:

function sayHello () {
  alert("Hello");
}
sayHello ();

The above is equivalent:

(function () {
  alert("Hello");
})();

Both are the same, it is just the preference of coding style.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252