I always thought explicit ; is important due to human factors, and ASI only exists to reduce frustration caused by inadvertent omission of line-end, but it appears some people even use tools to get rid of non-mandatory ;?
Also, sometime ASI works by checking whether a second statement would be broken if ; wasn’t inserted before the statement. Won’t this penalise performance if happens too often?
What would a situation where ; is best to be avoided be like?
There are no technical reasons (I can think of) where it’s better to not use semicolons. Not using semicolons is a stylistic preference, not a technical one.
Enforcing semicolons on the other hand does have a few technical reasons. There are a few edge cases surrounding AST you have to know about, but there aren’t that many. Leaving them off is fairly safe as long as you know what you are doing
No performance difference at all. Purely preference of style.
The argument for semicolons is that it’s easier to not make a mistake. There are situations in JS where something you intend to be two separate statements will be interpreted as a single statement without a semicolon. So a lot of people just go with the rule of using semicolons always that way you never run into issues.
The argument for not using semicolons if you don’t need them is that it’s simply less syntax. Why add more characters to your code if it’s not necessary? Removing semicolons removes noise from the code and looks cleaner. Testing and linters can protect you from the caveats of not using semicolons, plus you could just learn how ASI works and it won’t be an issue.
Personally I prefer not using semicolons but I always just go with whatever is configured in the linter/formatter for the team and project I’m working on.
I admit I let Prettier do a lot of the formatting anyway. I might not always write all the semicolons (most but not all) and just let it insert them on save. I do prefer reading code with semicolons, but that’s just out of habit.
I feel like a lot of people that switched from using semicolons to not using semicolons did so when the more functional ES6’ish style of JS got popular. But as long as the code is well-formatted the lack of semicolons doesn’t really bother me too much.
It can mess a bit with your pattern recognition if you are not used to reading it though. It’s the same with languages that are indentation sensitive as opposed to using code blocks with brackets. Be it SASS or Python, etc. my brain can’t really handle it. Clean or not, it just looks like something is missing. Again, this is just a habit.
A fun use case but not a realistic one. Sounds like you’re referring to a type of “code golf” which actually semicolons are useful because they allow you to squish all your statements together which is done to eliminate whitespace characters (something we’re not trying to do when writing regular production code).
There was a time in the earlier days of writing software that things like character counts in your source code actually mattered because of storage limitations but that was a long time ago.
I wonder if there might be some accessibility considerations in play here? There may be some advantages to including the semicolon that would benefit people with reading/language/cognitive disorders. I think it would be hard to read a paragraph of text with no periods, even if each sentence started on a new line. I can’t help but think it might be harder for some people to read code without something signifying the end of a statement, especially if their text editor is wrapping the lines.
I always use semicolons but I’m not going to demonize someone who doesn’t. I’m just wondering aloud if the slight inconvenience of using them might be beneficial to certain people?