DEV Community

Do you use semicolon in JS?

Javien Lee on June 05, 2022

Do you usually use semicolon when you program? And why? In my case, I don't use semicolon. I know how ASI(Auto Semicolon Insertion) works and it's...
Collapse
 
stephanie profile image
Stephanie Handsteiner

When I join a codebase I use whatever the codebase defines, to keep it consistent within.

As for my own projects, yes, I always use semicolons and have setup my linting config in a way to respect that. I just like my semis! ๐Ÿ™‚

Collapse
 
fjones profile image
FJones

This. Evidently, adapt to the prevalent style in the codebase, and be consistent with it. But I also prefer using them and enforcing them through linting rules. In fact, I'm currently in the process of adding linter rules to the codebase I was just put in charge of, and semicolons are on the list of rules to enable as soon as I have time to resolve the existing inconsistencies.

Collapse
 
javien profile image
Javien Lee

It's true linter makes us not fall into conflict๐Ÿ˜‚ And as one of the people who uses semicolons, may I ask why you prefer to use semicolons?

Thread Thread
 
fjones profile image
FJones

For a start, semicolons are more readable - they terminate a statement, regardless of indentation confusing things. But also because, for me at least, omitting them takes more thought than just adding them, so it's a timesink, too.

Collapse
 
javien profile image
Javien Lee • Edited

Frankly, it's not a big deal to follow the rules they already use. Thank you ๐Ÿ‘

Collapse
 
lexlohr profile image
Alex Lohr

Semicolons are part of the standard; use them if you want or don't, but please keep it consistent.

Collapse
 
javien profile image
Javien Lee

thank you for sharing your opinion :)

Collapse
 
jankapunkt profile image
Jan Kรผster • Edited

I personally don't use semicolons and also enforce them to be absent, using standard as my linter (precisely standardx to allow a few customized settings).

I already got so used to it that writing with semicolons feels annoying to me.

Collapse
 
javien profile image
Javien Lee

100% agree. And it's a good news that I'm not the only one who feels awkward when using semis๐Ÿ˜‚

Collapse
 
drsensor profile image
เซฎเผผโš†๏ธฟโš†เผฝใค

No-semicolon seems a must when writing variable declaration in Elm/Haskell style.

const S = Symbol
    , sData = S()
    , sEffect = /* @__PURE__ */ S()
    , sComponent = /* @__PURE__ */ S()
Enter fullscreen mode Exit fullscreen mode

Changing the order require to remove and add semi-colon if semi-colon is enforced by the linter or formatter.

Collapse
 
mistval profile image
Randall

I use semicolons. I guess I'm just used to it. It feels weird to me to not use semicolons. For a beginner, or someone who is not already pretty good at JS, I recommend always using semicolons. Otherwise it's only a matter of time until you run into a really confusing error caused by "incorrect" AIS and have to beg for help. No-semicolons is cool and all (I guess?) but learn the language very well first, or you are asking for trouble.

Collapse
 
javien profile image
Javien Lee

It can be really confusing for beginners. I think, however, it will be an option to use it after understanding the principles of ASI. ๐Ÿค—

Collapse
 
jzombie profile image
jzombie • Edited

I used to care a lot about this, until I started working on a mix of projects w/ and without them, and just leave it up to the linter to fix the changes on save.

With that in mind, I tend to not use them when I know the linter will just do its thing, but if there happens to not be a linter involved I will usually insert them because it feels purer to me.

Collapse
 
crongm profile image
Carlos Garcia โ˜… • Edited

I've always used semicolons. The first time I heard about this new trend of not writing them, to be honest I found it quite dumb, like it made no sense. When I asked for the reasoning behind it on Twitter the only two kind of answers I got were "because they're not necessary" and "it saves you time". I wondered, how many seconds can you save in a day by not writing semicolons?

Then I landed on a project which codebase did not use semicolons. I did not bat an eye to it and just went along with being in a "modern" and "cool" project.

That is, until we got around to define our linting and auto-fix rules, when everyone agreed semicolons should be added on file formatting. What was the point then?

I guess it all comes down to personal preference, but as others have said, consistency is the most important thing.

Collapse
 
joelbonetr profile image
JoelBonetR ๐Ÿฅ‡

I use them, mostly auto inserted by Prettier (format on save).
I feel it more readable for humans as you can find easier where the statements end without extra effort.

Collapse
 
j471n profile image
Jatin Sharma

Nope, vs code does it for me ๐Ÿ˜‚

Collapse
 
merb profile image
merb

I like to use standard js eslint rules in my personal projects. Will use whatever rules that have already been established for a project though.

standardjs.com/

Collapse
 
hrayrpetrosyan profile image
Hrayr Petrosyan

Let's imagine someone wrote the first snippet. It will take him forever to debug the problem.

Collapse
 
vulcanwm profile image
Medea

I use them when I feel like it makes the line neater

Collapse
 
praneetnadkar profile image
Praneet Nadkar

I saw a similar title of a post somewhere.
"Do you use semicolons in JS or you are a Demon ?" :D

Collapse
 
javien profile image
Javien Lee

Yes, totally, this is one of old discussions. I should go checkout that post rn๐Ÿ˜Ž

Collapse
 
bfunc profile image
Pavel Litkin

Use no semicolons in personal projects, because it makes code looks cleaner to me and use whatever is defined in corporate ones.

Collapse
 
yxsh profile image
Yash • Edited

I do use them actually. not always but when I feel like this line needs a semicolon then I use it

Collapse
 
gabrielpedroza profile image
Gabriel Pedroza

Not really; either way, everyone's code will compile with semicolons because of ASI(automatic semicolon insertion)

Collapse
 
highasthedn profile image
highasthedn

Definitely yes! I don't know why but a coding line doesn't look "finished" without a semicolon to my eyes

Collapse
 
ivadyhabimana profile image
Ivad Yves HABIMANA

Personally I like semicolons for the code readability especially when it's an unfamiliar codebase but for my code I don't type them directly I have the formatter or the lint add them for me

Collapse
 
javien profile image
Javien Lee

That's a kind of good way. Thanks for letting me know๐Ÿ˜„

Collapse
 
bcowley1220 profile image
Brendan Cowley

No.
Outside of the fact that I don't have to, it's a waste of time and is unnecessary.