A few reasons to use semicolons when writing JavaScript

Semicolon is the statement ending character in many programming languages.

1 var counter = 0;

It is used in Java, C/C++, C#, PHP and Perl name a few languages. You really can't have missed it if you have been programming even a little while.

In JavaScript however, it is not mandatory to use semicolons. Newline characters terminate statements according to specific rules. A bit old but good recap along with opinionated guidelines is presented here by Node.js core contributor Isaac Schlueter.

There is a divide between developers on whether to use semicolons or note. There are people for and against it. There exists even an npm module called Standard that goes so far as to state the so called standard is not to use semicolons.

This is going to be a plea for the opposite. Please do use semicolon to end a statement even though it is not necessary. It will give you plenty of benefits. They all have to do with code readability. Indirectly they relate to code maintainability, code quality and the amount of bugs lurking in the code.

Visual cue for the eyes

When reading program code, your eyes look for visual cues to group related parts together. You can achieve this by using line breaks, punctuation, white space, comments and what have you.

Semicolon as a statement ending character is one of the ways to help your eyes navigate in program code.

Imagine if regular text prose had no punctuation marks. Reading a paragraph of sentences without ending period would be a bit painful. Try the following:

Here is a sentence without ending periods You can probably read and understand it But it doesn't mean it is really that pleasant to read Especially if there is another capitalized word like Xerox or something

Not that you often write multiple program statements on one line. And, to credit Isaac, certainly you wouldn't bring all rules from English language to programming. However, ending a statement definitely helps your eyes to see what is going on in the text.

The statement is finished

A statement expresses a basic unit of computation. It describes a set of instructions to be executed. You naturally want to end describing it and indicate that it's complete. You want to say that there are no more characters to be included in the statement. This is all there is to it. As a programmer I am moving over to the next one. Achieve a sense of closure.

Load contents from the file named data.txt. Period. Increment the counter by one. Period. Render the index page template. Period.

A statement ending character suits to this purpose perfectly.

Newline has divided responsibilities

If no separate statement ending character is used the role of is solely left to the newline character. It has divided responsibility as it is used also not representing a statement ending but also regular formatting.

For example object literal is best split to multiple lines for better readability. In that case newlines are not statement endings but only formatting. This sends mixed signals to the user, be it unconsciously or not.

There are caveats

Since semicolons are optional, the rules with newline characters take higher priority over semicolons. The following will return undefined even if you are using semicolons. It can be counted as one of JavaScript's idiosyncrasies and is best avoided.

1 function returnsUndefined() {
2     return
3         "value";
4 }

So use semicolons for readability but avoid these traps that stem from newline terminated statements.

Node doesn't wait for your database call to finish?

Node doesn't wait for your database call to finish?

Learn how asynchronous calls work and make your app run as you intended. Get short email course on asynchronicity and two chapters from Finish Your Node App.

Take Free Course