Semicolons in JavaScript, what are they good for?
Everything you need to know.
Typing More
Some people enjoy typing out an extra character on every line.
It goes without saying you'll also get to read slightly more.
Harder to Read Diffs
This attribute of semicolons will help you keep code reviewers on their toes.
Harder to Read Diffs When Removing a Chained Method
Semicolons | No Semicolons |
---|---|
This change now shows as affecting two lines, when it really only affects one. I bet our reviewer didn't see that one coming when they spent time scanning both lines for changes ;).
Harder to Read Diffs When Adding a Chained Method
Semicolons | No Semicolons |
---|---|
This change now shows as affecting two lines, when it really only affects one.
Making It More Difficult to Move Around Lines
Semicolons | No Semicolons |
---|---|
Shuffling lines is easy, especially if you use vim. If you'd like to swap the last and second last chained methods you need to manually fix the semicolon. This ensures you don't accidentally modify your code.
Preventing Bugs in Cases That Will Never Come up If the Code You're Writing Isn't Terrible
A bug caused by lack of semicolons! Surely it will take us hours to figure out what's wrong. 1
Do you write code like this? If you do you'll love semicolons.
Conclusion
If you like making it needlessly more difficult to type out, modify, and code review, your code, then you should use semicolons in JavaScript.
For more info see Four Things to Avoid in JavaScript.
Here's what it looks like after formatting with Prettier:
You should use Prettier, with default settings obviously since you'll definitely want semicolons, especially after reading all this.↩