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!
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:
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
}
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;
}
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;;
};
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!
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:
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.