Do You Recommend Using Semicolons After Every Statement in JavaScript

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 I use semicolons in JavaScript?

Use them. Use them constantly.

It's far too easy to have something break later on because you neglected a semi-colon and it lost the whitespace which saved it before in a compression/generation/eval parse.

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)

Are semicolons mandatory in javascript statements?

Semicolons are not always mandatory, but I would always recommend using them. See the ECMAScript spec for the rules on automatic semicolon insertion:

Certain ECMAScript statements (empty statement, variable statement,
expression statement, do-while statement, continue statement, break
statement, return statement, and throw statement) must be terminated
with semicolons. Such semicolons may always appear explicitly in the
source text. For convenience, however, such semicolons may be omitted
from the source text in certain situations. These situations are
described by saying that semicolons are automatically inserted into
the source code token stream in those situations.

Update (to explain further)

Perhaps the most common situation used to show why automatic semicolon insertion can be bad is that touched on by @sissonb in another answer. Consider the following:

function something(a, b) {
return
a + b;
}

What you may be expecting is for the new-line to be ignored, and the code interpreted as:

function something(a, b) {
return a + b;
}

Unfortunately, automatic semicolon insertion comes into play, and the code is actually interpreted like this:

function something(a, b) {
return;
a + b;
}

And an empty return statement means the function returns undefined. So instead of a nice sum of the two argument, you get undefined and potentially end up very confused as to where you've gone wrong! Which is why I completely agree with the statement in your question that automatic semicolon insertion is a horrible misfeature.

  • Example (returns undefined because of ASI).
  • Example (returns expected result).

Are semicolons needed after an object literal assignment in JavaScript?

Not technically, JavaScript has semicolons as optional in many situations.

But, as a general rule, use them at the end of any statement. Why? Because if you ever want to compress the script, it will save you from countless hours of frustration.

Automatic semicolon insertion is performed by the interpreter, so you can leave them out if you so choose. In the comments, someone claimed that

Semicolons are not optional with statements like break/continue/throw

but this is incorrect. They are optional; what is really happening is that line terminators affect the automatic semicolon insertion; it is a subtle difference.

Here is the rest of the standard on semicolon insertion:

For convenience, however, such semicolons may be omitted from the source text in certain situations. These situations are described by saying that semicolons are automatically inserted into the source code token stream in those situations.

Why so many semicolons in JavaScript?

Many computer languages use semicolons to denote the end of a statement. C, C++, and Java are popular examples of this.

As for why people use them despite them being optional, they improve the readability of your code. In most cases it's simply done out of habit, but occasionally you need semicolons in your code to remove possible ambiguity. It's always better safe (and consistent) than sorry.

Here is an example taken from Do you recommend using semicolons after every statement in JavaScript?

// define a function
var fn = function () {
//...
} // semicolon missing at this line

// then execute some code inside a closure
(function () {
//...
})();

This will be interpreted as:

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

Additionally, semicolons allow Javascript to be packed/minified properly. Otherwise all the statements will be mushed together into one big mess.

Why use semicolon?

It looks like there are very few reasons, or, actually, edge cases, when one would want to use semicolons.

http://aresemicolonsnecessaryinjavascript.com/ <- this is down now, use

https://github.com/aresemicolonsnecessaryinjavascript/aresemicolonsnecessaryinjavascript.github.com

Should I place a semicolon at the end of my jQuery statement?

Always put the semicolon. Not only is it more cross-browser compatible, it's easier to minify (removing newlines is part of that).



Related Topics



Leave a reply



Submit