loading...

Discussion on: Here are Top 5 Best Practices for JavaScript you MUST ALWAYS FOLLOW

Collapse
savagepixie profile image
SavagePixie

This feature is known as Automatic Semicolon Insertion. However, using this feature is not recommended

I would be very interested in a link to whomever it is that doesn't recommend omitting semicolons. Is it ECMAScript? Is it a general consensus that I can find somewhere? Or is it a personal preference?

Semi colons are also crucial for better readability.

Can you provide some examples in which this is the case?

I'm not trying to challenge your point, I am honestly interested in the semicolon debate. It's just that I've never seen any compelling reason to use them.

Collapse
umerkk164 profile image
Umer K Author

To the first part, this is the official styling guide by google
here
Note that ECMA Script does not force you to explicitly type semicolons. It does not recommend relying on ASI either.

To the second part, I might have to dig up some examples, but in general writing codes passing function expressions as parameters, object literals as parameters, and nested function calls can get pretty messy and even if pretified, without semicolons denoting end of statements, it is hard to follow code.

Collapse
savagepixie profile image
SavagePixie

To the first part, this is the official styling guide by google

But that's just a style guide to ensure that Google code follows the same conventions all throughout. It seems to me far from any sort of official/widely agreed on recommendation. In fact, if we were to take the document's guidelines as best practices, it clearly contradicts your last point.

To the second part, I might have to dig up some examples, but in general writing codes passing function expressions as parameters, object literals as parameters, and nested function calls can get pretty messy and even if pretified, without semicolons denoting end of statements, it is hard to follow code.

Maybe if the code is very messy. Personally I've never had trouble with those. Well, at least not any trouble that I thought would be solved or improved by adding semicolons.

Collapse
adam_cyclones profile image
Adam Crockett

Google is Google it is not the overarching authority on JavaScript style. You are better placed following the lint rules of your project. ASI works but knowing where to put semi's is the problem for some people. I use semi because I follow my project nfr's, I used to omit them, now I don't care and I recommend both ways. They both work fine.

Collapse
couch3ater profile image
Connor Tangney

For some additional information on whether you should / shouldn't rely on ASI, check out this post!

Collapse
blindfish3 profile image
Ben Calder

The point is to understand where automatic semicolon insertion is going to cause bugs and avoid code that will allow it (preferably with lint rules) and then be consistent. The classic example is to avoid a return statement that splits the returned value to a new line.

Anyway I'll add a sixth best practice you should always follow:

Never follow advice that says you should always follow it. There are always exceptions :)