JavaScript semicolon: to your taste

Original author: Kent C. Dodds
  • Transfer
Using semicolons in JavaScript is one of the most discussed topics (right after spaces and tabs ... two spaces, please). Here are three links on the fly , why semicolons are not needed. But is it really so?



Transpilation and Uglification


The very first thing you should know about semicolons is such a thing as “Automatic Semicolon Insertion (ASI)”. This is a feature that, in fact, allows you to discuss the necessity or unnecessary points with semicolons. Read about it if you have not already done so. Like Kyle ( post ), I believe that you should not trust ASI. This is not a good idea, for many reasons.

The problem with complex ASI logic disappears as soon as you start using a transpiler or minifier. For example, Babel and UglifyJS2 automatically add a semicolon to the generated code.

When they tell me that semicolons can not be written, I agree - as long as the guys are sure that during the deployment these semicolons will be returned back.

Linting the bad parts (untranslatable pun)


There are some very unpleasant moments in the logic of ASI. But as long as you use ESLint with the no-unexpected-multiline key , you are safe. It is enough to make sure that your build system does not assemble the project if one of the developers violated the rule. You might also be interested in another rule, semi .

Why refuse semicolons?


In view of the foregoing, now there is no need to discuss in what cases semicolons can be used, and in which it is impossible. Now this is a matter of taste for the developer.

To put or not to put semicolons is a personal preference

Why do I prefer not to use semicolons? Not because my right little finger is broken. And not because I like to print one character less. The thing is that I don’t want my editor and linter to tell me that I need to insert something that I really don’t need (so I told them that I do not want semicolons - and now they , on the contrary, they warn me when semicolons still appear in my program).

Also, when I write code, I prefer to focus on the problem. And so as not to distract me from her, the need to add something that has nothing to do with her. After I started to ignore semicolons (and got used to how “awful” the code looked at first), I perceive my code as more “clean”.

Reasons to use a semicolon?


In her post, Kyle discusses not only ASI, but its own preferences regarding the use of semicolons. Highly recommend reading.

In my opinion, what is important in what Kyle said is that he considers the semicolon a guarantee of a more “unambiguous” and readable code. Especially for novice developers. Perhaps this is so, but for me the code did not become less ambiguous or readable after I abandoned the semicolons. And now I don’t even think about them.

conclusions


If you do not use a transporter / linter, I strongly recommend that you do not give up semicolons (in this case, we are not talking about personal preferences - this will just be the correct use of JavaScript). I highly recommend using either a transporter, or a linter, or both. In this case, you can use or not use semicolons, and this will be only your preference - the code will not suffer in either case. See you on Twitter !

Also popular now: