A close friend and I got into an argument a couple days ago because he wholeheartedly believes (ECMAScript) code without semi colons equals bad practice for reasons I understand completely but don't agree with. (we fight about important things like this all the time)
I personally write JavaScript without semi colons and am are very opinionated about it. The same goes for more people here on devtoo. I found that for instance moji has none either.
There is a wide abundance of discussions and people arguing about it on the internet so.
Where exactly in the ECMAScript specification does it say that semi colons are omitted/required or not?
Top comments (12)
JavaScript explicitly has rules for automatically inserting semicolons. Hence, they aren't necessary.
However, the rules aren't very good, in that they are likely to do the wrong thing.
For example, consider the following:
It doesn't do what you mean, because the semicolon insertion rules say to put a semicolon here:
Your expression's result is lost!
If you set your IDE up to automatically insert semicolons / make your CI tools reject code that would have semicolons inserted automatically, you can avoid these mistakes.
Here's the deal. Pick semi or no semi. Use a linter and format on save. Stop worrying about it.
One I started formatting on save, I stopped caring as much.
Just add long as your code is consistent across the whole project, that is already a win.
This is the correct answer. Linter will take care of the edge cases and warning you if something is wrong.
I think its fine to code without, but like the steering committees are moving towards, we are not sure where the language will be in 2-20 years. So the issue is, do you want your code to survive intact without modification (bad idea, but a very slim use case) in 10 years? Then you probably want to use semicolons. At the least have an IDE or tool push them in.
I believe their guidance is that they want to reduce clutter and what they call 'syntactic sugar' so they recommend not using them, although I think semi-colons actually increase readability. Especially when it's not your own code and you are reviewing it.
Here:
ECMAScript 2019 Language Specification, Section 11.9.
While you can write JS code without semicolons, you'll run into cases where you'll have to replace them with something different that is easily forgotten, like the following bug:
each line by itself will work, but together, they will fail, because the IEFE will be interpreted to be an argument to the function output of the first line. To fix that, you'd have to put a
!
in front of the IEFE or a semicolon at the end of the first line.Since it is easier to forget the first than to remember the second - at least if you put a semicolon at the end of each statement, you'll see why this is a best practice.
#ShamelessPlug
Inflammatory Topics & Being A Reasonable Dev...
Chad Windham
When I read current ECMAScript specs (1.5ish years ago) it clearly states semicolons are optional... That is because of automatic insertion when it is parsed out. People will point to very contrived situations where not having a semicolon causes an error, but they are few and far between. Also, there are lots of situations where using a semicolon doesn't do what you think it will and there is still a parsing error (also usually very contrived examples...). In the situations where it will throw an error just understand the rules and put in a semicolon, not hard once you understand. But the idea the "just using semicolons at the end of every statement" just magically makes everything "safer" is more rooted from years ago when some parsers didn't honor the language specs properly, but they do now. There is a reason semicolon free JS is becoming more and more prevalent.
Because it is fine and within the specs of the language.
But if code sacrificed to semicolons causes your brother to stumble, do the loving thing and throw in some semicolons...
The problem is the both sides of the argument are correct. You can't convince devs who abide by them to not use them, because they are right. You can't convince devs who believe you can omit them they are wrong, because they aren't!
I feel a little tripped up coding in Swift, which doesn't require have semicolons but really looks like it should.
Spent the whole Friday morning aguing with someone about just that
I just let prettier insert semicolons for me
As much as I agree with you, I also enjoy the debate.