DEV Community

Jochem Stoel
Jochem Stoel

Posted on

Semi colons;

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)

Collapse
 
curtisfenner profile image
Curtis Fenner • Edited

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:

return
    firstLongExpression(thatTakesManyArguments, andPushesMoreToTheNextLine)

It doesn't do what you mean, because the semicolon insertion rules say to put a semicolon here:


return;
firstLongExpression(thatTakesManyArguments, andPushesMoreToTheNextLine);

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.

Collapse
 
joelnet profile image
JavaScript Joel

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.

Collapse
 
davidszabo97 profile image
Dávid Szabó

This is the correct answer. Linter will take care of the edge cases and warning you if something is wrong.

Collapse
 
thefakeandres profile image
Andres R

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.

Collapse
 
fnh profile image
Fabian Holzer • Edited

Where exactly in the ECMAScript specification does it say that semi colons are omitted/required or not?

Here:
ECMAScript 2019 Language Specification, Section 11.9.

Most ECMAScript statements and declarations must be terminated with a semicolon. Such semicolons may always appear explicitly in the source text. For convenience, however, such semicolons may be omitted from the source text in certain situations. These situations are described by saying that semicolons are automatically inserted into the source code token stream in those situations.

Collapse
 
lexlohr profile image
Alex Lohr

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:

console.log('the next line will produce an error)
(function() { console.log('this is a normal IEFE') })()

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.

Collapse
 
vitalcog profile image
Chad Windham

#ShamelessPlug

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...

Collapse
 
vitalcog profile image
Chad Windham

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!

Collapse
 
ben profile image
Ben Halpern

I feel a little tripped up coding in Swift, which doesn't require have semicolons but really looks like it should.

Collapse
 
yorodm profile image
Yoandy Rodriguez Martinez

Spent the whole Friday morning aguing with someone about just that

Collapse
 
itsjzt profile image
Saurabh Sharma

I just let prettier insert semicolons for me

Collapse
 
jochemstoel profile image
Jochem Stoel

As much as I agree with you, I also enjoy the debate.