Javascript self-invoking functions

Example of a self-invoking javascript function. Notice the parenthesis around the entire function declaration.

(function() {
    var foo = 20;
})();

The parenthesis around the function exist to differentiate it from an actual function definition. It would not be possible to auto-invoke it with the () at the end.

Another syntax for self-invoking functions is also available:

! function() {
    var foo = 20;
}();

Again, the ‘!’ is there to show that its not a function declaration.