Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Semicolons after <em>function declarations</em> are <strong>not necessary</strong>.</p> <p>The grammar of a <code>FunctionDeclaration</code> is described in <a href="http://ecma262-5.com/ELS5_HTML.htm#Section_13" rel="noreferrer">the specification</a> as this:</p> <pre><code>function Identifier ( FormalParameterListopt ) { FunctionBody } </code></pre> <p>There's no semicolon grammatically required, but might wonder why?</p> <p>Semicolons serve to separate <em>statements</em> from each other, and a <code>FunctionDeclaration</code> is not a <em>statement</em>.</p> <p><code>FunctionDeclarations</code> are evaluated <em>before</em> the code enters into execution, <a href="https://stackoverflow.com/questions/1710424/referencing-a-javascript-value-before-it-is-declared-can-someone-explain-this/1710509#1710509">hoisting</a> is a <em>common word</em> used to explain this behaviour.</p> <p>The terms "function declaration" and "function statement" are often wrongly used interchangeably, because there is no function statement described in the ECMAScript Specification, however there are some implementations that include a function statement in their grammar, -notably Mozilla- but again this is non-standard.</p> <p>However semicolons are always recommended where you use <code>FunctionExpressions</code>, for example:</p> <pre><code>var myFn = function () { //... }; (function () { //... })(); </code></pre> <p>If you omit the semicolon after the first function in the above example, you will get completely undesired results:</p> <pre><code>var myFn = function () { alert("Surprise!"); } // &lt;-- No semicolon! (function () { //... })(); </code></pre> <p>The first function will be executed immediately, because the parentheses surrounding the second one, will be interpreted as the <code>Arguments</code> of a function call.</p> <p>Recommended lectures:</p> <ul> <li><a href="http://kangax.github.com/nfe/" rel="noreferrer">Named function expressions demystified</a> (great article)</li> <li><a href="https://stackoverflow.com/questions/1634268/explain-javascripts-encapsulated-anonymous-function-syntax">Explain JavaScript’s encapsulated anonymous function syntax</a> (more on <code>FunctionDeclaration</code> vs <code>FunctionExpression</code>)</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload