Coder Perfect

In javascript, why should I add a semicolon after each function?

Problem

In javascript, I’ve seen some devs use semicolons after functions while others don’t. Which method is the most effective?

function weLikeSemiColons(arg) {
   // bunch of code
};

or

function unnecessary(arg) {
  // bunch of code
}

Asked by macca1

Solution #1

Semicolons after function declarations are not necessary.

In the specification, the grammar of a FunctionDeclaration is described as follows:

function Identifier ( FormalParameterListopt ) { FunctionBody }

Although there is no grammatical requirement for a semicolon, you might question why.

Semicolons are used to separate statements, and a FunctionDeclaration is not one of them.

FunctionDeclarations are assessed before the code is executed; the term “hoisting” is often used to describe this behavior.

Because there is no function statement defined in the ECMAScript Specification, the phrases “function declaration” and “function statement” are frequently used interchangeably. However, certain implementations, most notably Mozilla, include a function statement in their language, but this is non-standard.

Semicolons, on the other hand, are always suggested when using FunctionExpressions. Consider the following scenario:

var myFn = function () {
  //...
};

(function () {
  //...
})();

If you remove the semicolon after the first function in the preceding example, you’ll get the following:

var myFn = function () {
  alert("Surprise!");
} // <-- No semicolon!

(function () {
  //...
})();

Because the parentheses enclosing the second function are understood as the Arguments of a function call, the first one will be run immediately.

Recommended lectures:

Answered by Christian C. Salvadó

Solution #2

After function-as-variable declarations, I use them:

var f = function() { ... };

but not after definitions in the classical style:

function f() {
    ...
}

Answered by Gabe Moothart

Solution #3

JS Lint is the de-facto standard, and it states that there should be no semicolon after the function body. See the “Semicolon” section for more information.

Answered by David Hedlund

Solution #4

Just stick to your guns! Although they aren’t required, I use them because most minification procedures rely on the semi-colon (for instance, Packer).

Answered by Josh Stodola

Solution #5

It all boils down to personal preference. I like to end lines of code with semi colons because I’m used to Java, C++, C#, etc, so I use the same standards for coding in javascript.

However, I don’t usually conclude function declarations with semi colons, but that is just my own choice.

Browsers will work in either case, but maybe one day they’ll come up with some more stringent guidelines.

I’d write the following code as an example of what I’d do:

function handleClickEvent(e)
{
     // comment
     var something = true;  // line of code
     if (something)  // code block
     {
        doSomething();  // function call
     }
}

Answered by regex

Post is based on https://stackoverflow.com/questions/1834642/why-should-i-use-a-semicolon-after-every-function-in-javascript