Is there any difference between
(function (){alert('')} ())
vs
(function (){alert('')}) ()
Both works but when should I use each ?
The wrapping parentheses are only there to force the parser to parse the construct as a function expression, rather than a function declaration. This is necessary because it's illegal to invoke a function declaration, but legal to invoke a function expression.
To that end, it doesn't matter where the invoking parentheses go. It also doesn't matter how you force the function to be parsed as an expression. The following would work just as well:
!function () {
alert('')
}();
~function () {
alert('')
}();
// Any unary operator will work
If you decide to use the wrapping parentheses (grouping operator), then just bear in mind that JSLint will tell you to move the invoking parentheses inside. This is simply a stylistic choice and you can ignore it if you want.