DEV Community

Should you use semicolons in JavaScript?

Nico Zerpa (he/him) on June 22, 2022

It's a topic that could seem trivial at first sight, but it actually causes some controversy among JavaScript developers. Semicolons, yes or no? T...
Collapse
 
lexlohr profile image
Alex Lohr •

There's a worse issue with omitting semicolons; consider the following code:

(() => { console.log('first') })()
(() => { console.log('second') })()
Enter fullscreen mode Exit fullscreen mode

This will throw a TypeError that undefined is not a function, because the brackets around the second IEFE are interpreted as a function call. A semicolon between these lines will solve the issue.

Collapse
 
frankwisniewski profile image
Frank Wisniewski •

mmh.... what is an IEFE?

Collapse
 
lexlohr profile image
Alex Lohr •

Immediately executed function expression.

Thread Thread
 
frankwisniewski profile image
Frank Wisniewski •

aha, I've only encountered IIFE so far...
(immediately invoked function expression)
I think we mean the same thing

Collapse
 
tracygjg profile image
Tracy Gilmore • • Edited

Greeting from the UK.
I do not see this question much these days but it was a common topic of discussion a few years ago. It was a similar "hot topic" to indent using tabs or spaces.
I have tried developing using both styles and (if you know what you are doing) both styles can work. However, I personally find using semicolons more natural and consistent with code I write in other programming languages.
In all the professional grade code I have reviewed over the last four-five years, using semicolons was the preferred style.

Collapse
 
seancassiere profile image
Sean Cassiere •

I don't add semicolons while programming, rather I wait till I'm done, hit save and let Prettier add them in for me.
I mostly keep the semicolons in the saved files, since I don't want the built code to have any issues with immediately invoked functions in Javascript.

Collapse
 
michaelmangial1 profile image
Michael Mangialardi •

I don't mean to provide a weak answer, but to me, consistency is more important (and this can be achieved by automatic code formatting).

Collapse
 
mthompsoncode profile image
Mark Thompson •

Not a weak answer. Consistency is the most important thing for linting-related issues. Both standards are completely valid. The only thing I would consider is the team's background and preference.

Collapse
 
pengeszikra profile image
Peter Vivo •

5 years ago I also feel semicolon boring. But now I use as sign to myself: I finished the working and thinking this line of code.

Collapse
 
lexlohr profile image
Alex Lohr •

Yes, I know. It's probably the least bad solution in these cases.

Collapse
 
tylim88 profile image
Acid Coder •

Don't forget dynamic method name

Collapse
 
baukereg profile image
Bauke Regnerus •

"It's a topic that could seem trivial at first sight"

Also on second and third sight.

Collapse
 
jonrandy profile image
Jon Randy 🎖️ •

I used to use them, and now only do so when necessary