Semicolons in JavaScript - The Definitive Guide

July 18, 2016

Programmers often disagree as to whether semicolons in JavaScript are a good or bad thing. Clearly it depends on what you are programming and how important it is. Here is a simple guide!

Semicolons in JavaScript - The Definitive Guide

There is much debate (for some reason) as to whether or not one should use semicolons throughout their JavaScript code, or only when it is absolutely required.

Those arguing to leave them out claim they aren't needed, and so leaving them out is cleaner and more succinct. Semicolon advocates have many reasons for their inclusion, but the one stated most often (and with the most UPPER CASE) is that it is much safer to always include them.

Clearly we can have the best of both worlds, if we simply regulate our usage based on the importance of the code at hand!

Here is my handy definitive guide:

Casual Code

When: Use this style for quick hacks and brogramming your Arduino-based ghostbusters proton pack. Its reckless, wild and free. You are the Easy Rider of coding!

Style: No semicolons anywhere (until you get a bug, then add one on the buggy line). 

var a = 10, b = 20

function add(a,b)
{
	return a + b
}

Important Code

When: For projects with code that should actually work. You know, for your day job.

Style: Semicolons at the ends of code lines, but not block start/ends/function defs. If in doubt, put a semicolon in.

var a = 10, b = 20;

function add(a,b)
{
	return a + b;
}

Really Important Code

When: For projects dealing with money or sensitive data. Extra semicolons are a small price to pay for the security of knowing this code is bug free!

Style: Double semicolons on code lines. Semicolons peppered elsewhere "to be sure".

var a = 10, b = 20;;
;
function add(a,b)
{;
	return a + b;;
};

Hardened Code - Top Quality Required

When: For Maximally Important, Fort Knox secure, weapons grade coding. When a bug could result in a summary execution.

Style: Double semi-colons everywhere - at beginning AND end of lines. Protective Wall at start and end of code sections. If in doubt, add a semicolon. If not in doubt, add a semicolon!

;;;;;;;;;;;; // protective wall of semicolons
;;var a = 10, b = 20;;
;;
;;function add(a,b)
;;{;;
	;;return a + b;;;;;;;; // this function MUST work
;;};;
;;;;;;;;;;;; // protective wall of semicolons

There you have it. Print this handy guide out and carry it with you everywhere. Pull it out at hacking parties to show other programmers you know how to wield the all-powerful semicolon like a pro!

Final Note

For the slightly humor-impaired, this is not a serious guide. It is a humorous exaggeration of the fallacies sometimes propogated by adamantly pro-semicolon advocates.

Here are a few other "good" (not good) reasons to use lots of semicolons in your JavaScript:

  • ASI Rules are Complicated - so its safer to just always use semicolons.
  • Not using semicolons breaks JSHint.
  • Really, how lazy can you be to try and save the typing of one character!?
  • If you don't want to use semicolons, why don't you just use CoffeeScript?
  • Come on! People who don't use semicolons are just showing off how well they know the language!
  • JavaScript uses semicolons to end statements! Deal with it!
  • Would you read a book without punctuation in it?
  • But Douglas Crockford says we should always use them!

Further Reading / Viewing

For a (mostly) more serious take on the subject, I recommend this blog post by Isaac Z. Schlueter. Also this video titled "Semicolons cannot save you!" by the always excellent Mattias Petter Johansson is well worth a watch.


If you or your organization could use a somewhat fanatical developer on your current or next project, lets talk. I am passionate about the user experience, easy to work with, and I get stuff done. Lets build something great!


Semicolons in JavaScript - The Definitive Guide Tweet This!
Glenn is the founder of bluejava K.K., and has been providing consulting services and developing web/mobile applications for over 15 years. Glenn lives with his wife and two children in Fujisawa, Japan.
Comments

No comments yet. You can get the conversation going!

Add Comment
Displayed alongside comment
Optional. Not displayed, but used for avatar
Optional
Don't enter anything here: