Minimal anonymous JavaScript closuresEdit

Putting content inside a closure (ie. by putting it in a function) allows you to create temporary variables without polluting the global scope. However, if you name your function, the name itself will pollute the scope, and if you make your function anonymous, you’ll get a syntax error:

// not valid syntax!
function() { /* ... */ }();

By preceding your anonymous function with a bang, you turn it into a function expression with valid syntax:

!(function () {
    var foo, bar, baz;
    // etc...
})();

This is a byte shorter than the alternatives with valid syntax:

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

See also