DEV Community

Cover image for Semicolon or No semicolon?
Sumit Kumar Surana
Sumit Kumar Surana

Posted on

Semicolon or No semicolon?

I have 10 years of experience being a JavaScript developer and I forget that semicolon is optional. Adding a semicolon in the end is a simple instinct for me.

From my point of view, if you are using the right tools like jshint or jslint to catch the syntax errors before pushing the code to your repository, it doesn't matter if you use a semicolon or not.

I use semicolons in the end because I am tuned that way while working with professionals or mentors who end the statement with a semicolon. I also advocate using semicolons in the end because of the following reasons.

Automatic semicolon assertion

I won’t spend too much time explaining about it since it is really well articulated in ecma specs https://tc39.es/ecma262/#sec-automatic-semicolon-insertion.

To put it simply, it is better to handle the semicolon ourselves than relying on a compiler/minifier which has a chance of being unpredictable. Especially, when you are using ‘[‘ or ‘(‘ or binary operator or a regex in the next line without ending the previous statement with a semicolon. Refer https://tc39.es/ecma262/#sec-asi-interesting-cases-in-statement-lists.

I prefer lesser risk on my production code and I believe in the statement that if something can go wrong it will definitely go wrong.

Consistency

Most developers nowadays are full stack developers who have dabbled in one or more backend languages. And most of the popular backend languages (apart from python that I know of) have statements ending with semicolon.

So when a developer tends to switch between other programming languages and JavaScript they don't have to unlearn the idea of not putting the semicolon in the end. Remember it is optional. So no offense if someone puts it or omits it. But the idea is to keep a single standard and consistency so that it doesn't cause any confusion among team members on what to follow. So make sure you choose one or else brace yourself for resolving lots of ideology clashes among the team members like the discussion going on at https://davidwalsh.name/javascript-semicolons.

Top comments (11)

Collapse
 
burtek profile image
Bartosz

There are cases where a semi is always required, i.e.

const data = fn1();
(await fn2()).something
Enter fullscreen mode Exit fullscreen mode

I prefer using semis always rather than having to use them as exception, which IMO break the code style

Collapse
 
bradtaniguchi profile image
Brad

My usual stance on this is whatever my auto-formatter adds automatically.

Prettier has an option to toggle this behavior on and off.

Few argue about spacing in code because it's usually automatically added/removed via tooling. As long as it's consistent within the codebase it usually isn't an area of contention.

I believe semi-colons should be in the same boat. Just let the computer add/remove/apply them. The few edge-cases should be handled by other tooling, like TypeScript.

Collapse
 
sayan3990 profile image
Sayan Bhattacharyya

Thanks it is something new to me

Collapse
 
moopet profile image
Ben Sinclair

Auto-formatted by what? I've never seen auto-formatting do that before.

Collapse
 
eljayadobe profile image
Eljay-Adobe

I use Standard JS. q.v. my other post reply.

In an Edna Mode voice, "No semicolons!"

Josh Perez No, you don't need semicolons. And when a semicolon is necessary, I use Max Bendick Semicolons go at the beginning of the line.

Collapse
 
alaindet profile image
Alain D'Ettorre

I advise to always use them.

I know that Python and JavaScript are the most popular ones and it's cheap to skip them, but since most languages use them anyway, it's better to use them for me. Besides, I think the code is a bit clearer with semicolons.

Collapse
 
rxliuli profile image
rxliuli

It's not what format that matters, it's just one format, for that matter, I configure the npm package with a custom prettier preset

github.com/rxliuli/liuli-tools/tre...

Collapse
 
bcowley1220 profile image
Brendan Cowley

Nope, stopped using em a while ago and never looked back. Fecking linters put them back in anyways so who cares.

Collapse
 
ivan_jrmc profile image
Ivan Jeremic

I think they exist because there was a time without syntax highlighting and you need to see where the end is.

Collapse
 
boyney profile image
Boyne

I mean you can put semicolons in Python if you want everyone to hate you

Collapse
 
ethand91 profile image
Ethan

I always use semicolons. Got so used to it that I have to keep reminding myself to stop using them in a language like python... xD