DEV Community

Why you should use semicolons in spite of ASI

Adam Crockett πŸŒ€ on January 26, 2020

What side of the road do you drive on? In England we say it's the right side which is of course left. This is enforced by the police, if I decided...
Collapse
 
chasm profile image
Charles F. Munat

No.

Collapse
 
adam_cyclones profile image
Adam Crockett πŸŒ€

How dare you πŸ˜‚. I can't even remember writing this must have been a long time ago. Good that you know your mind

Collapse
 
daniel13rady profile image
Daniel Brady • Edited

πŸ’― agree. When writing, we should always be optimizing for clear communication, not less words. Writing code is hard in large part because we're optimizing for clear communication to both humans and machines.

If you care about communicating in a particular way, then write in a language that embraces that style of communication. Using the handle of a screwdriver to bang in a nail when you've got a hammer within reach will work, but might make people go πŸ€¨πŸ€”when they see what you're doing.

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt

I think that, on the safe side, always add semicolon; but the only real time I need semicolon, is when the line starts with a bracket

  • IIFE for scope declaration
  • Type casting in TypeScript, which requires brackets to be able to add .

In the end, it is more about being consistent, but it is easier to be consistent on the safe side.

Collapse
 
xowap profile image
RΓ©my πŸ€–

I totally agree. Not deciding if I should put semicolons is one less problem in my life, if there is cases where I need them then I always need them.

Collapse
 
osde8info profile image
Clive Da

OMG ; are optional in CSS ! must learn those rules ASAP (as an ex PHPer i hate ;)

Collapse
 
adam_cyclones profile image
Adam Crockett πŸŒ€

Here is how that works

.not-in-cansis {
    background: red;
    color: green
}

But seriously don't.
Collapse
 
osde8info profile image
Clive Da

too late i LOVE it i will write some prettier rules straight away

Collapse
 
lamka02sk profile image
lamka02sk

Only after the last property and it is not a good idea to leave them out. Your colleagues are going to eat you alive.

Collapse
 
dbshanks profile image
Derek Shanks

I always pop my semi-colons in. I do run back and forth between PHP and JS it just makes sense. I let prettier handle the moments when I am dolt that forgets to put one in.

This is also largely why I struggle with languages like Ruby and Elixr, I love my braces and brackets to go along with semi-colons too. Makes me feel snug as a bug.

Collapse
 
evanplaice profile image
Evan Plaice

Understanding how a language parses code is difficult enough without ambiguity like ASI

I always use SemiStandard to lint my OSS projects

github.com/standard/semistandard