Semicolons in JavaScript, what are they good for?

Everything you need to know.

June 4, 2020

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

SemicolonsNo 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

SemicolonsNo Semicolons

This change now shows as affecting two lines, when it really only affects one.

Making It More Difficult to Move Around Lines

SemicolonsNo 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.


  1. 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.


Dylan Vann
Software developer living in Toronto 🇨🇦 Thailand 🇹🇭.
Focused on Node | GraphQL | React | React Native.
Subscribed!
You should receive an email with a confirmation link!
Join my Newsletter
I'll send you an email when I have something interesting to show.
Or follow me on Twitter for more stuff like this.