The Great Semicolon Debate

Recently there has been a large debate about the use (or rather, the lack of use) of the semicolon in JavaScript code. A few words that shouldn’t have been said were said and suddenly people are at each other’s throats. The idea of omitting semicolons isn’t entirely new, but it has hit its stride recently, and it’s starting to crash into the opinions of the traditionals. Here, I’m giving my thoughts on this whole debacle.

To start things off I’m going to be very forthcoming: I do not like the idea of purposefully omitting semicolons – or other syntactic characters, for that matter. I have several reasons for this opinion:

  1. Your production (minified) code doesn’t shrink in size, so there are no speed benefits
  2. The semicolon is technically required; automatic semicolon insertion (ASI) is designed to save the butt of an unaware developer
  3. Raise the possibility for buggy code just so you can save typing a character every so often
  4. Syntax helps developers see intention

Minified JavaScript is Unaffected

My first point is on the lack of benefits gained from the practice of omitting semicolons. You might save some bytes in your production code, but that’s not what matters. If you’re using it on a finished product you should be using the minified code, which is unaffected by your lack of semicolon usage. A smart minifier will have ASI built into it so that you will have the semicolons you need in the minified code, so either way the semicolons are there. Besides the lack of speed gained in the minified code, if you did use the code without the semicolons in production code, it’s slightly slower because the compiler has to determine your intention and insert the proper semicolons for you.

Semicolons Are Required in JavaScript

As stated in the bullet points, ASI is not an excuse to be lazy and omit semicolons on purpose. ASI is designed to prevent errors in otherwise inerrant code and should not be abused for the sake of making your code look better (and it doesn’t really look any better anyway). Semicolons were included as syntax in order to delimit statements, so use them.

Buggy Code

By deciding to omit semicolons, you must learn additional rules to follow in your program, and that learning time (and space in your brain) could be put to better use learning the rest of the language better. If you omit a semicolon where one needs to exist in order to prevent a parsing error, then you waste time having to go back and add it in. You spend too much time making sure that you can skip the semicolon, which pulls your mind away from what’s truly important: the program itself.

Syntax is Your Friend

Syntax is there to help people and parsers determine your intentions. When omitting a semicolon, the reader of your code may have to spend time they otherwise wouldn’t spend determining whether a line break is entirely for aesthetic reasons or as a delimiter between statements. Be a pal and use the proper syntax please.

Bringing this Fiasco to a Finish

The best thing I can do is try to persuade you to do what I believe is the right thing, but in the end the choice is up to you. On this site I will strive to always use semicolons, and any semicolons missed are purely on accident. I see no real benefits from purposefully omitting semicolons, so if you think there is a legitimate reason that deserves mentioning, I’d like to hear it in the comments. I will almost always side with Douglas Crockford (the man who wrote JavaScript: The Good Parts, has done countless JavaScript seminars, and is generally considered to be the one of the greatest minds in JavaScript), but if I don’t, it’s either because he has given me no good reasons to side with him, or I have more good reasons to not side with him than he does to side with him.

As a final note and announcement: The contest to win a $299 Wijmo license is still ongoing. The contest ends May 8th, so make sure you sign up before then.

Author: Joe Zimmerman

Author: Joe Zimmerman Joe Zimmerman has been doing web development ever since he found an HTML book on his dad's shelf when he was 12. Since then, JavaScript has grown in popularity and he has become passionate about it. He also loves to teach others though his blog and other popular blogs. When he's not writing code, he's spending time with his wife and children and leading them in God's Word.