⬆️ ⬇️

JavaScript semicolon: to your taste

Using semicolons in JavaScript is one of the most hotly discussed topics (right after spaces and tabs ... two spaces, please). Here are three links on the move, why semicolons are not needed. But is it really?







Transpilation and Uglification



The very first thing you need to know about semicolons is something like “Automatic Semicolon Insertion (ASI)”. This is a feature that, in fact, allows us to discuss the necessity or uselessness of semicolons. Read about it, if you have not already done so. Like Kyle ( fasting ), I think you shouldn’t 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 minifix. For example, Babel and UglifyJS2 automatically add a semicolon to the generated code.

')

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



Linting the bad parts (untranslatable word game)



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 any of the developers broke the rule. You might also be interested in another rule, semi .



Why refuse semicolons at all?



In view of the foregoing, it is no longer necessary to discuss in which cases semicolons can be used and in which cannot. Now this is a matter of taste of the developer.



To put or not to put a semicolon - personal preference



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



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



Reasons to use a semicolon?



In his post, Kyle discusses not only ASI, but also his own preferences regarding the use of semicolons. I strongly recommend reading.



In my opinion, in what Kyle said, it is important that he considers the semicolon as the key to a more “unambiguous” and readable code. Especially for novice developers. Perhaps this is true, but for me the code did not become less unambiguous or readable after I refused to use commas. And now I don’t even think about them.



findings



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

Source: https://habr.com/ru/post/283304/



All Articles