// I need to

ModernizeUpgrade without breaking it RescueFix what someone else started Build NewTurn an idea into a product Reach More PeopleGet found, get used, include everyone

// Company

Case StudiesExpertiseAbout + CareersResourcesContact Book a call →
≡articleBuild New

Style Wars

Are semicolons optional in JavaScript? Only sometimes. Weighing the factors in the JavaScript semicolon style debate before picking a side.

Art+Logic Apr 19, 20164 min read

From the Art+Logic archive · written in 2016. The tools may have moved on; the thinking still holds.

I recently realized that semicolons might be optional in JavaScript. I guess this is something I kinda sorta always knew but didn’t want to think about. JavaScript is a mess of proprietary non-standards, right? Well, maybe ten years ago. It’s okay to care about the syntax now that it’s reliably deterministic.

So should I include them or omit them? Obviously the Spirit of the Art & Logic Style Guide would have the semicolons included. But I still need to ask myself the question for …reasons. I’m not going to be satisfied without understanding all the factors.

But wait, why are there factors? Why isn’t it common knowledge that everyone should stop typing all those “extra” semicolons, case closed? Well, it turns out that semicolons are only optional sometimes.

Apparently I was supposed to have this conversation in 2012 when there was a big disagreement. Sorry I’m late. Regardless, I notice a lot of slanted misinformation is still widely available on the Internet, as usual. So I’m hoping to help the situation some more by talking about only neutral, practical, real-world reasons for choosing a style.

(Disclaimer: My reasons are not really unbiased.)

The aforementioned disagreement isn’t worth reading because it’s full of that slanted misinformation. Instead, you should check out this article:

http://inimino.org/~inimino/blog/javascript_semicolons

It just explains the JavaScript specification (the only place where it’s called ECMAScript, strangely enough) neutrally and lets you come to your own conclusion. Really, it’s worth reading. (It says the pertinent section in the ECMAScript spec is 7.9, but as of this writing it’s 11.9.)

So the JavaScript specification says semicolons are required, but “for convenience” may be omitted at the end of a line, usually. The spec includes “Automatic Semicolon Insertion” rules covering all possible situations.

Understanding the rules eliminates superstitions. Some people say these rules are ambiguous, or that browsers are free to interpret them ambiguously. That’s obviously bunk, or else too many more web pages would be broken than are already. Moving on.

Real reasons against semicolons:

Real reasons for semicolons:

I highly recommend you try the opposite of your usual practice on some real-world code just to see what happens. Then you won’t be just speculating, and you might learn something unpredictable. Incidentally, the minification tool I’m using at the moment is completely unaffected by this decision; the outputs are 100% identical.

You may notice I didn’t mention anything about the JavaScript syntax possibly becoming more strict in the future. That’s because that’s not a real concern; sloppy JS will always be supported by browsers just as sloppy HTML continues to be supported. That’s how we got into this “optional semicolons” mess in the first place!

I also didn’t mention the special rule affecting return statements. That’s because that’s a separate problem that isn’t directly solved by the use of semicolons.

What I learned from all of this is that there are several exceptional cases where semicolons need to be included even though they’re optional usually. For me, that’s the biggest giveaway. It means that a practice of omitting them requires extra vigilance against human error, which simply isn’t worth it. In addition, the exceptional cases help illustrate that the underlying intention of JavaScript is for the semicolons to be there, and deliberately omitting them is a rebellion you might have to take back someday. Once again, my advantage in perceiving this situation is due to experience in a variety of languages. Omitting semicolons in JavaScript is inconsistent and goofy compared to other languages where they really are optional.

Of course, another factor is whether or not other people are ever going to look at your code. For somebody out there at some time, the factors I listed above will weigh in favor of omitting semicolons, and that’s okay. As long as I really won’t have to fix your code someday.

Either way, you should actually learn the syntax and erase superstitions.

* * *

Incidentally, That JavaScript Guy won the big disagreement. Not that that should necessarily sway you.

keep going

More to explore in Build New.

All Build New resources →
let's talk

Got a problem like this one?

Tell us what's stuck. A real person replies within one business day.

Talk to our team →

Talk to people who know software, not a sales script.

Your first call is with Aja, our VP of Sales. He'll help make sure you're solving the right problem. Here's what you'll cover together:

  1. What you're trying to accomplish

    The business goal first, then the technology.

  2. Possible approaches

    And the tradeoffs of each, in plain language.

  3. Risks you may not have considered

    Technical and business, before they cost you.

  4. Whether AI changes the math

    What's practical, and what's just hype for your project.

  5. Sensible next steps

    Even if the right next step is not us.

book-a-call.ics

Loading the scheduler…

Open the booking page →

Some problems are easier to explain in writing. Send us a few lines about what you're working on, and we'll reply within one business day. Helpful to include:

  1. What the software does, or should do
  2. What's stuck, or what's worrying you
  3. Any deadline or budget you have in mind
note.md

Loading the form…