November 16, 2015
Now that I use prettier, it’s really a
matter of what you like to look at (because prettier means that I don’t have to
type them at all). I prefer the way code looks without semicolons so… :) I just
eslint-config-prettier and let
prettier deal with it.
Transpilation and/or Uglification
The first thing that you need to know is something called Automatic Semicolon Insertion (ASI). It's the "feature" that allows us to even have this conversation. Read up on that if you're not familiar. Like Kyle's (post), I agree that you should not rely on ASI. It's a really bad idea for many reasons.
The problems with relying on ASI go away when you transpile or minify your code (depending on your technology). For example, transpiling with Babel will add the semicolons back and uglifying with UglifyJS2 will too.
So for me to say you're good to go on omitting semicolons in your source code, you first need to make sure that whatever you end up shipping to production (whether browser or node) has the semicolons added back.
Linting the bad parts
There are a few gotchas with ASI. However, if you are using ESLint and you enable the no-unexpected-multiline rule, then you're safe. Just make sure that your build pipeline will fail if that rule is broken because most assuredly your app will! You may also be interested in the semi rule.
Why omit semicolons anyway?
With these things in place, this is no longer a discussion about what works and what doesn’t but becomes a simple matter of preference.
Omitting semicolons is a matter of preference
So why do I prefer to not have semicolons? It’s not just that I have a broken right pinky (though sometimes it gets hurt) or I like typing one less character per line. It’s simply because I don’t like my linter/editor telling me I need to add something that is not necessary (so I tell them I don’t want semicolons and they warn against them instead).
Also, I like to stay focused on the problem, not worrying about adding or removing something that doesn’t really matter in the end. And since I’ve started omitting semicolons (and gotten used to how ugly it looks at first) I actually feel like it leaves my code looking cleaner (you’ve just gotta be untrained to think that you need semicolons).
Why should you use semicolons?
I should note that Kyle’s post (linked earlier) is not arguing only about ASI but he’s also arguing his reasoning for his own preference (using semicolons). I recommend you give that a read as well.
One notable thing that Kyle mentions in that post is he feels like code with semicolons is more explicit and clear (and in a conversation with him he mentioned to me that it’s lacking empathy for newcomers). That could be true, but for me I don’t feel like my code has gotten any less clear, maintainable, or readable since I removed semicolons from it. And now I don’t have to even think about it.