DEV Community

Semicolons in JavaScript: To Use or Not to Use?

adriennemiller on April 02, 2019

During one of our first JavaScript lectures at the Flatiron School, the instructor mentioned that semicolons are optional in JavaScript… except whe...
Collapse
 
somedood profile image
Basti Ortiz • Edited

Personally, I write all of my code with semicolons. Not only because I have grown used to it, but it's also more readable for me because I don't have to think about where statements end. There's just this subconscious part of me that can't interpret code unless there are semicolons. It's analogous to how difficult it is to read a paragraph without proper punctuation, particularly the period.

But then again, that's only my opinion. That's simply how my brain works. We all have different ways of thinking. Using semicolons should not only be a matter of style and consistency but also a matter of lessening the cognitive load of programmers for the sake of productivity and overall happiness.

Collapse
 
adriennemiller profile image
adriennemiller

I agree! Coming from Ruby, I find the semicolons and parenthesis in JavaScript comforting and helpful for readability.

Collapse
 
zyzmoz profile image
Daniel Cunha (he/him)

I totally agree, using semicolons make you code more readable! Maybe, in case of new developers, it is just a matter of get used to it!

Collapse
 
hasnaindev profile image
Muhammad Hasnain • Edited

This is a stylistic choice. Someone coming from a Python background won't use it. I've worked on codebases from large companies that DON'T use semi colon and I have no issue with the readability because code readability has nothing to do with semi colons and everything to do with the "good code/bad code" philosophy.

Collapse
 
brigittavarga profile image
Brigitta Varga

I agree with this, I also use semicolons every time. I guess I‘m also used to it, but also because (to me for sure but I think also for others) it‘s more readable.

Collapse
 
elmuerte profile image
Michiel Hendriks

I think putting significance in things you cannot print is a mistake.

Programs must be written for people to read, and only incidentally for machines to execute. -- Abelson & Sussman

So it's always semicolons for me. Because then I'm clear that this is where I want the statement to end. For the same reason I do not use naked ifs.

Collapse
 
outlawgametools profile image
J. A. Whye

I know this is an older article, but I didn't want someone coming here, reading all the comments and thinking, "Oh, everybody uses semicolons, I guess I'd better..."

Not everybody does. Why type something you don't need to?

I program in C# for my job so am required to use semicolons at the end of the lines. But at home when I'm working on something in Lua, or Javascript, I skip the semicolons. They are generally NOT needed so I don't include them.

If your boss says you have to use them, then use them, but otherwise, why bother?

Collapse
 
dlopez831 profile image
David Lopez (Argen.)

yes... Why bother writing comments if the compiler / interpreter ignores them?

Collapse
 
rapmendoza profile image
Rap Mendoza

Hey man, I understand your point - but doesn't it help if you have something less to think about, such as these rules, and focus on the things that actually matters? I mean you can predict better at what would happen in your code without thinking about such rules.

Collapse
 
elarcis profile image
Elarcis

I prefer using semicolons, always:

  • It's easier to just put one without wondering if it's needed
  • I'm more comfortable with formating tools when they add stuff than when they remove it.

I even push it further, and always use trailing commas when they're at the end of a line (like Prettier does with the "trailingComma": "all" setting), mainly because adding items to arrays and argument lists only highlights the new lines in source control (while not using trailing commas also highlights untouched lines where a comma was added).

Collapse
 
renato76 profile image
Renato Mignogna • Edited

Interesting that most people like using semicolons. Personally I do not like using them at all, I actually find them a little bit annoying haha!

I can clearly see the end of each line or statement, my linter makes me break long lines into multi lines that area easily readable, so I can't really see the point of adding these little annoying things at the end of each line, it certainly does not make code more readable for me. It's a matter of opinin I guess, and also what each individual is more used to.

dev-to-uploads.s3.amazonaws.com/up...

Collapse
 
fchaplin profile image
Frederic CHAPLIN • Edited

I tried a project without semicolon. 
It's a full-stack project, with typescript/node/express on backend and React/JavaScript on frontend. 

Here are my conclusions on it :

  • I encountered no compiling error so far. 
  • I had no readability issues. My code felt cleaner and more readable to me without the "semicolon noise".
  • But the next developer on the project may find this disturbing

I will use the semicolon again, and stick to the good practices for now. Mainly because I'm afraid of the dreaded compiling bug that may occur and because I care for others.

But I clearly think that JavaScript is much better without semicolons.

Collapse
 
fasani profile image
Michael Fasani

I just wanted to say: google.github.io/styleguide/javasc...

Relying on implicit insertion can cause subtle, hard to debug problems. Don't do it. You're better than that.

Omitting them is simply a bad practice whatever your reasons.

Collapse
 
riekus profile image
Riekus van Montfort

I just wanted to express my appreciation for the Kurt Vonnegut reference. Also, semicolons, because more readable

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt

Personally, I write all of my code with semicolons. and also "", so that I can use IIFE, especially async IIFE.

Double quotes, so that I don't forget when I program in other programming languages.

Also, double space, rather than Tab.

Never have been in a corporate env where everything is forced, though.

Collapse
 
sreramk profile image
Sreram K • Edited

I am a beginner to JavaScript, but from reading this I think it will be unacceptable to not use semicolon whenever possible!

It makes sense to omit semicolon in a language like python as every line break functions as a statement terminator. But if the statement termination is being inferred from the grammar rules, this is where you must ask: why is this even allowed in the first place?

Collapse
 
eterychan profile image
Eternia • Edited

Your reason is one which makes sense the most; by comparing it with Python. I can live without semicolons if the language I am using disregard them to begin with. But if the language actually needs them and the interpreter adds them for me by following three simple rules; I would rather add them myself!

Collapse
 
zimaben profile image
Benny T

I don't find semicolons any more readable than their absence...but when I skip them I generate a lot more errors when I'm coding in other languages.

I don't mind the extra keystroke as much as I do clearing like 8 500 errors on a page because i zoned into no-semis mode.

Collapse
 
bsorrentino profile image
bsorrentino

Thanks for this article

I had underestimated that question assuming it was a negligible detail

Collapse
 
balachalasani profile image
Bala Chalasani

There is a typo in point 1 example 2 in the ASI interpreted code:

Should be

var a;
b = 5;

Instead of:

var a;
b = 3;

Collapse
 
adriennemiller profile image
adriennemiller

Thanks, updated!

Collapse
 
vuongtran21 profile image
vuongtran

My thinking is still using semicolons for convention and readable code. I'm writing application using Vuetify, and default eslint setting without semicolons. That make me feel not comfortable with it.

Collapse
 
kenyo profile image
Ken Yo

Good reading!