!function(){ }() vs (function(){ })()

  • One less character when minified.
  • The ! should handle where other JavaScript code is concatenated before this and doesn’t have a trailing semi-colon.

There is not a huge difference. I would use whatever you were more comfortable with. You should probably toss something at the start of your example to avoid…

base.js

var lol = function() {
   alert(arguments[0]);
}

im-concat-to-base.js

(function() {
    // Irrelevant.
})();

jsFiddle.

Toss in a leading ; and she works…

jsFiddle.

…or a ! like the Twitter Bootstrap…

jsFiddle.

Leave a Comment