title: Explain the encapsulated anonymous function syntax
body: <h2>Summary</h2>
  
  <p>Can you explain the reasoning behind the syntax for encapsulated anonymous functions in JavaScript? Why does this work: <code>(function(){})();</code> but this doesn't: <code>function(){}();</code>?</p>
  
  <hr>
  
  <h2>What I know</h2>
  
  <p>In JavaScript, one creates a named function like this:</p>
  
  <pre><code>function twoPlusTwo(){
      alert(2 + 2);
  }
  twoPlusTwo();
  </code></pre>
  
  <p>You can also create an anonymous function and assign it to a variable:</p>
  
  <pre><code>var twoPlusTwo = function(){
      alert(2 + 2);
  };
  twoPlusTwo();
  </code></pre>
  
  <p>You can encapsulate a block of code by creating an anonymous function, then wrapping it in brackets and executing it immediately:</p>
  
  <pre><code>(function(){
      alert(2 + 2);
  })();
  </code></pre>
  
  <p>This is useful when creating modularised scripts, to avoid cluttering up the current scope, or global scope, with potentially conflicting variables - as in the case of Greasemonkey scripts, jQuery plugins, etc.</p>
  
  <p>Now, I understand why this works. The brackets enclose the contents and expose only the outcome (I'm sure there's a better way to describe that), such as with <code>(2 + 2) === 4</code>.</p>
  
  <hr>
  
  <h2>What I don't understand</h2>
  
  <p>But I don't understand why this does not work equally as well:</p>
  
  <pre><code>function(){
      alert(2 + 2);
  }();
  </code></pre>
  
  <p>Can you explain that to me?</p>