title: JavaScript anonymous function immediate invocation/execution (expression vs. declaration)
body: <blockquote>
    <p><strong>Possible Duplicates:</strong><br>
    <a href="https://stackoverflow.com/questions/1013385">What is the difference between a function expression vs declaration in JavaScript?</a><br>
    <a href="https://stackoverflow.com/questions/1634268/explain-javascripts-encapsulated-anonymous-function-syntax">Explain JavaScript&#39;s encapsulated anonymous function syntax</a>  </p>
  </blockquote>
  
  
  
  <p>Why this:</p>
  
  <pre><code>(function () {
      //code
  }());
  </code></pre>
  
  <p>and this:</p>
  
  <pre><code>var f = function () {
      //code
  }();
  </code></pre>
  
  <p>works, while this:</p>
  
  <pre><code>function () {
      //code
  }();
  </code></pre>
  
  <p>does not? It looks exactly the same - anonymous function defined, and immediately called. Can someone make a quotation from the JavaScript/ECMAScript standard which explains that?</p>
  
  <p><strong>UPDATE</strong>: Thanks for the answers everyone! 
  So it's about <strong>function expression vs. function declaration</strong>. See <a href="https://stackoverflow.com/questions/1634268/explain-javascripts-encapsulated-anonymous-function-syntax/1634321#1634321">this Stack&nbsp;Overflow answer</a>, <a href="http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf" rel="noreferrer">ECMAScript standard</a> section 13, and this great article: <em><a href="http://kangax.github.com/nfe/" rel="noreferrer">Named function expressions demystified</a></em>.</p>
  
  <p>To recap answers:</p>
  
  <ol>
  <li><p>The first snippet is interpreted as an expression because the grouping operator, <code>()</code>, is applied - see <a href="http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf" rel="noreferrer">ECMAScript standard</a> section 11.1.6.</p></li>
  <li><p>In the second snippet, the function is interpreted as an expression because it's on the right-hand part of assignment operator, <code>=</code>.</p></li>
  <li><p>The third snippet doesn't have anything which allows the interpreter to read the function as an expression, so it's considered a declaration, which is invalid without an identifier (Gecko lets it pass however, but it chokes on following <code>()</code> grouping operator (as it thinks) applied to nothing).</p></li>
  </ol>