Member-only story
You Might Need Those Semicolons in Your JavaScript After All
Leaving them out looks pretty, but it could break things
When I first started writing JavaScript I thought that semicolons were mandatory. I was learning about jQuery at the time and all of the documentation I was reading showed a semicolon at the end of every statement.
I had a bit of background in some other programming languages and this was consistent with what I already knew — semicolons are there to help the computer distinguish one instruction from another. Makes sense.
It wasn’t long before I learned about JavaScript’s Automatic Semicolon Insertion (ASI). Basically, JavaScript will put semicolons in for you automatically if you leave them out. So, they aren’t mandatory after all.
The history behind this is interesting. JavaScript didn’t start out as the programming language heavyweight that it is today. In the beginning, it was intended to be something that non-professionals could pick up without too much trouble. ASI was a feature thrown in to make the language more friendly to laymen. At least, that was the idea.
To avoid breaking the internet, JavaScript needs to be backward compatible. We can add new features (ES6, ES7, ES8, etc) but we can’t take features away because they’re already being used and relied on. Whether intentionally or by accident, millions of lines of JavaScript exist that need ASI to work properly. So, we’re all stuck with it, but some of us choose to handle it in different ways.
One camp writes code that includes semicolons. Their code looks a bit more like a language that actually requires semicolons, like C or PHP.
The other camp leaves the semicolons off, relying on ASI to put them in automatically. Their code looks slightly more like Python or Ruby.
The JavaScript community is still split on this. For example, React still includes semicolons in its docs, but Vue does not.
Vue was my next stop in JavaScript frameworks after jQuery and React, and I decided to adopt the no-semicolon coding style featured in the docs. I enjoyed it (it makes your code look so much cleaner!) and began applying that style to other JavaScript endeavors outside of Vue —…