So should I include them or omit them? Obviously the Spirit of the Art & Logic Style Guide would have the semicolons included. But I still need to ask myself the question for …reasons. I’m not going to be satisfied without understanding all the factors.
But wait, why are there factors? Why isn’t it common knowledge that everyone should stop typing all those “extra” semicolons, case closed? Well, it turns out that semicolons are only optional sometimes.
Apparently I was supposed to have this conversation in 2012 when there was a big disagreement. Sorry I’m late. Regardless, I notice a lot of slanted misinformation is still widely available on the Internet, as usual. So I’m hoping to help the situation some more by talking about only neutral, practical, real-world reasons for choosing a style.
(Disclaimer: My reasons are not really unbiased.)
The aforementioned disagreement isn’t worth reading because it’s full of that slanted misinformation. Instead, you should check out this article:
Understanding the rules eliminates superstitions. Some people say these rules are ambiguous, or that browsers are free to interpret them ambiguously. That’s obviously bunk, or else too many more web pages would be broken than are already. Moving on.
Real reasons against semicolons:
- Less clutter.
- Intimidates other programmers!
Real reasons for semicolons:
- JS is based on C language where the semicolons are required. “Automatic Semicolon Insertion” was actually a compensation to allow sloppy code.
- If you simultaneously program in other languages based on C besides JS, consistency in style would be helpful to you.
- Exceptional cases requiring semicolons don’t need to be minded. The syntax is complicated compared to Lua or (legacy) T-SQL, which don’t have such exceptional cases. (And the best practice for exceptional cases is to prefix the line with a semicolon, which I personally despise.)
- More explicit programmer intent. Of course, this matters the most when I’m fixing your broken code, but it could also matter when you’re fixing your own code.
- Some minification tools are broken and won’t handle your source right without the semicolons.
- Some minification tools insert semicolons to put the whole JS on one line, reversing your omissions.
I highly recommend you try the opposite of your usual practice on some real-world code just to see what happens. Then you won’t be just speculating, and you might learn something unpredictable. Incidentally, the minification tool I’m using at the moment is completely unaffected by this decision; the outputs are 100% identical.
I also didn’t mention the special rule affecting return statements. That’s because that’s a separate problem that isn’t directly solved by the use of semicolons.
Of course, another factor is whether or not other people are ever going to look at your code. For somebody out there at some time, the factors I listed above will weigh in favor of omitting semicolons, and that’s okay. As long as I really won’t have to fix your code someday.
Either way, you should actually learn the syntax and erase superstitions.
* * *