Here Is Why You Should Use Semicolons in Your JavaScript

The JavaScript community is still relatively divided about whether ending every statement with a semicolon is best practice or a waste of time.

Whether or not you decide to use semicolons largely depends on the existing code in your project. It is usually better to follow the pattern of the code in the area you are working, so unless you are starting a project from scratch, it doesn’t really matter much.

That being said, let’s see why the semicolon has an edge.

Argument for ditching them

Waste of time

Engineers often spend too much time on small nitpicks and miss the larger design issues. Let’s say, you are an engineer working on an e-commerce solution. You spend about 6 hours per day actually reading and writing code.

You open a pull request without semicolons and the team has decided they will use semicolons consistently. They review your code, make comments, and ask you to add them. Instead of taking 5 minutes to merge your pull request, you’ve spent an extra hour doing so.

Meanwhile, in all the fuss, the team missed a major design issue with your proposed solution.

Why write dead code?

If the semicolon is not needed, shouldn’t it be left out? We wouldn’t include an unnecessary module if it was not needed. Some people like to read only the code that will affect the end result.

Argument for using them

The main question the pro-semicolon folks ask is: Do we want the interpreter to have to guess where our statement ends?

Doing so can lead to some ambiguous code. For example, a developer might write the following code:

A human and a JS interpreter see these differently

They mean to define var fn as a function and then execute a closure function. However, the code is interpreted as the following:

They are unintentionally passing an argument to fn. At runtime, the second function will fail with a “… is not a function” error.

So why not just use a semicolon in that one case?

Fair point. Most teams I’ve worked on have chosen to use them to stay consistent but ultimately it is up to the project.

https://www.codydalton.me/