DEV Community

Javien Lee
Javien Lee

Posted on • Edited on

Do you use semicolon in JS?

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 rule makes my code more clean and readable.

Let me give you an example. The code below is an incorrect way to use ASI.

let foo = 'bar'
[1, 2].map(e => e + 1)
Enter fullscreen mode Exit fullscreen mode

because ASI will fix the code like this.

let foo = 'bar'[1, 2].map(e => e + 1)
Enter fullscreen mode Exit fullscreen mode

However, most of developer do not write code like this. In that code, we can't figure out what [1, 2] means. So we store it in a variable with name, that everyone can understand.

const ids = [1, 2]
const newIds = ids.map(e => e + 1)
Enter fullscreen mode Exit fullscreen mode

I'm not blaming people for using semicolons in their code, but I don't think there's a particular reason for me to use semicolons. Please share your opinion :p

Top comments (28)

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/