Why Should I Use a Semicolon After Every Function in JavaScript

Why should I use a semicolon after every function in javascript?

Semicolons after function declarations are not necessary.

The grammar of a FunctionDeclaration is described in the specification as this:

function Identifier ( FormalParameterListopt ) { FunctionBody }

There's no semicolon grammatically required, but might wonder why?

Semicolons serve to separate statements from each other, and a FunctionDeclaration is not a statement.

FunctionDeclarations are evaluated before the code enters into execution, hoisting is a common word used to explain this behaviour.

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.

However, semicolons are always recommended where you use FunctionExpressions. For example:

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

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

If you omit the semicolon after the first function in the above example, you will get completely undesired results:

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

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

The first function will be executed immediately, because the parentheses surrounding the second one will be interpreted as the Arguments of a function call.

Recommended lectures:

  • Named function expressions demystified (great article)
  • Explain JavaScript’s encapsulated anonymous function syntax (more on FunctionDeclaration vs FunctionExpression)

Do we need a semicolon after function declaration?

A function declaration does not need (and should not have) a semicolon following it:

function test(o) {
}

However, if you write a function as an expression, like the variable initializer below, then the statement should be terminated with a semicolon, just like any other statement would be:

var a = function test(o) {
};

See more about constructor vs declaration(statement) vs expression.

Do we need a semicolon after function declaration?

A function declaration does not need (and should not have) a semicolon following it:

function test(o) {
}

However, if you write a function as an expression, like the variable initializer below, then the statement should be terminated with a semicolon, just like any other statement would be:

var a = function test(o) {
};

See more about constructor vs declaration(statement) vs expression.

Do you recommend using semicolons after every statement in JavaScript?

Yes, you should use semicolons after every statement in JavaScript.

Do we need semicolon at the end?

The concept is known as JavaScript Semicolon Insertion or "Automatic Semicolon Insertion". This blog post: JavaScript Semicolon Insertion: Everything you need to know (archived from the original) outlines the concept well in an understandable manner using examples under the headings:

  • Where Semicolons are Allowed
  • Where Semicolons May be Omitted
  • The rules

It even digs into the official ECMAScript specification about the topic.

should or shouldn't we use semicolon after a function declaration inside a main function in Javascript?

Function declarations are no statements. They are not terminated by a semicolon, you should not use any.

If you put a semicolon there, it's parsed as an empty statement after the declaration.

However, not all function definitions are statements. myFunction is a variable that is assigned a function expression, and assignments are expression (or variable declaration) statements that are (should be) terminated by a semicolon.

function otherFunction() {

} // <-- no semicolon after declaration

var myFunction = function() {

}; // <-- semicolon after assignment

See also var functionName = function() {} vs function functionName() {} for more differences.

When should I use a semicolon after curly braces?

You use a semicolon after a statement. This is a statement:

var foo = function() {  alert("bar");};

Why doesn't JavaScript require semicolons after function declarations?

Semicolons serve to separate statements from each other, and a FunctionDeclaration is not a statement.

Why is JavaScript designed to automatically insert a semicolon after a return followed by a new line?

The exact reasons why are probably lost in the mists of time. I'm willing to bet that it happened something like this:

  • At some point, somebody thought it would be a good idea to make semicolons optional at the end of statements.
  • Later on, somebody else noticed an ambiguity in the syntax when semicolons were omitted when used with return statements in the way you describe.
  • The formal language specification was then amended with the confusing new rule about omitted semicolons on return statements, to codify current practice rather than changing the rules to make sense.


Related Topics



Leave a reply



Submit