It declares and calls/executes the function.
Consider function foo (){ ... }
If you just had that, it wouldn't do anything. In order to call foo
you need the parameter list (albeit null). So foo()
would execute.
The same thing applies to an anonymous function. However, if you have an anonymous function, like function (){ ... }
, how do you call it? You have no variable pointing to it. Without the end ()
it is just a definition, and not a call, so you must add the parens at the end, only you can't do function (){ ... }()
because of order-of-operation and how JavaScript is interpreted. Thus you also need to surround the definition in its own parentheses (function (){...})
. With the call it would look as you posted (function (){...})()
.
Note you can also define functions similarly, like:
var foo = function (){...};
Which essentially is creating an anonymous function and assigning it to a variable. You can then do foo()
like you would with any other function.