Why you should use semicolons in spite of ASI

Adam Crockett 🌀 - Jan 26 '20 - - Dev Community

What side of the road do you drive on?

In England we say it's the right side which is of course left. This is enforced by the police, if I decided to try a different strategy, middle or the right hand side, I would either die, or be detained, but before that, I could in fact drive for miles if nobody was on the road.

I will see some commenters saying follow the lint rules of your project (the police program of your codebase), end of. But I am going to look at it from the poor parsers perspective.

foo: {

}

// Vs

{
    foo: {
    }
}
Enter fullscreen mode Exit fullscreen mode

The grammer highlighter above is having a hard time with this. JavaScripts grammer is ambiguous as it is.

I could argue that it's polite to help the parser out with semicolons but I don't think that's enough, what I will say is when you don't uses semicolons you are relying on ASI automatic semicolon insertion to fill in the semicolons because you didn't bother, I get that, why write optional code? You got me yeah, write less sure, but what about context switching, you want to learn another language, I don't know, say Java, C++, C, Rust, PHP among others, these languages do not have ASI and you will need to add semis. So why not just get into the habit now?

I used to be the opposite, I have for a long time omitted semicolons, and in that time I learned the cases that I would need to use them. But why remember this at all, it's far simpler to train your muscle memory to just add semicolons.

Did you know that CSS has optional semi colons? But you didn't know that (probably) because it's under a very special circumstance, it's easier to just add them everywhere to make this parae and get on with your life, I have never seen anyone actually do it irl, not the case with JavaScript.

The last case, I have worked in 2 large companies, both use semi's as lint rules that say use them, so regardless of your preference, you are going to be using them. I feel there is a stronger case to enforce them to prevent errors during authoring and if a company told me otherwise I would fight hard to explain the cognitive load of remembering when and where to add semicolons in order for the parser to not mistake a () as a function call.

Tell me what your thoughts are down in the comments.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .