1

Possible Duplicate:
What does the exclamation mark do before the function?

So I've come across the use of the following by the twitter share button code:

!function (d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (!d.getElementById(id)) {
        js = d.createElement(s);
        js.id = id;
        js.src = "//platform.twitter.com/widgets.js";
        fjs.parentNode.insertBefore(js, fjs);
    }
}(document, "script", "twitter-wjs");

And I was just wondering what the !function bit does.

I'm guessing is short for (function(){...})(); but I couldn't find anything online to confirm it.

Community
  • 1
  • 1
Samuel Parkinson
  • 2,992
  • 1
  • 27
  • 38

1 Answers1

3

While @elias-van-ootegem got it mostly right, many browsers will consider it a syntax error without the bang.

function() { ... }() // Bad
!function() { ... }() // Good
(function() { ... })() // Better, more readable.
(function() { ... }()) // Best
Nate Higgins
  • 2,104
  • 16
  • 21