Are semicolons really optional in Javascript?

According to Semicolons in Javascript are optional they are.
If this is true, why are we using them?

2 Likes

It is a standard in Javascript, even though the syntax is not illegal. Standards are important if you want to work with other developers who expect you to put semicolons to know where your statements end. It also matters to put semicolons in order to tell Javascript how to execute the code statement by statement, otherwise it might interpret your code differently.

I always use semicolons in JS, out of habit from Java and C#.

Yup. If you are on my team and you commit code without semicolons* I will personally light your cubicle on fire.

* You actually can’t commit code to our repo without semicolons. We run tests for standards compliance.

3 Likes

You are completely fine without semicolons (Some other resources: 1, 2, 3). JavaScript Standard Style, which doesn’t use semicolons is used by many (6598 stars on GitHub at this moment, 59th most depended on dependency on npm).

The only gotcha with omitting semicolons is starting a line with (, [, or ` (JavaScript Standard Style doesn’t allow that) and since it’s mostly producing less readable code, it’s better to avoid it anyway.

It comes down to a coding standard of a project you are working on. You might not be able to commit a code without semicolons into ArielLeslie’s repo, but at the same time you might also not be able to commit a code with semicolons to some other repo.

1 Like

Yes, they are optional. The reason why people insert them themselves is because of automatic semicolon insertion. The gist of it being, if you want full control over where the semicolons are going to get inserted, you better write them yourself. There are a couple of caveats when not writing your semicolons, but i personally don’t have issues when omitting them.

Using or not using semicolons is more or less a matter of preference, a coding style. None is wrong, it is very useful when working in a team (even necessary) to decide on a certain style and then stick to it as a team.

1 Like

Thank you for all your replies, it’s clear to me now.
I think readability is the best argument to use them anyway.
(That is, besides Ariel’s promise to light ones cubicle on fire. :wink: )