Why is it necessary to wrap a function expression in parentheses to invoke it?
18:38 10 Mar 2013

In reading about javascript functions, I understand that you can call a function immediately after defining it thusly:

al = function(string){
      alert(string)
}("test");

but that you cannot do the same thing with empty parens:

al = function(){
      alert("test")
}();

and that, instead, you have to convert the function into a function expression:

al = (function(){
      alert("test")
})();

Why is this, and why does the first code example work correctly without this conversion?

javascript function