Are Semicolons Necessary in JavaScript?
aresemicolonsnecessaryinjavascript.com
aresemicolonsnecessaryinjavascript.com
It's all well and good until someone turns this:
if (conditional)
statement;
Into this without realizing what they've just done: if (conditional)
statement;
statement2;
Yes, you can skip semicolons. And yes it will work the vast majority of the time. But the amount of time you save will be eclipsed by that one time someone makes a change to the code that breaks in an unpredictable subtle way due to your purely aesthetic shorthand.Writing maintainable code is about learning humility. There's no need to show off your understanding of the deeper points of JavaScript semicolon insertion in your production code base. Just insert the semicolons, I promise you you will learn to not be distracted in no time.
If developer #2 isn't doing the most basic test to verify that "statement2" runs when "conditional" is true, then you've got way bigger problems than your style guide.
It's basically the birthday paradox. You take the probability of a very unlikely event and raise it to a big enough power (enough people, enough lines of code) it becomes a non-negligible outcome.
No, smart people miss it all the time.
a = b + c
(d + e).print()
. The author of the article says you can fix this by always putting a semicolon before any line-starting parentheses, like ;(d + e).print()
. Are you saying that you expect that rule to be too hard to remember for developers? If not, you should be able to give an example of another case where leaving out semicolons breaks things.Also, I agree that showing off your understanding of JavaScript parsing is a bad reason to leave out semicolons, but you imply that that is the reason everyone leaves out semicolons, and that is false. As you said, there are aesthetic reasons to do it – but having good aesthetics of code can (slightly) help one understand code more quickly, so it’s not pointless.
That article says that if a minifier doesn’t understand JavaScript that an actual JavaScript interpreter can understand, the minifier is buggy. I agree with it – if your minifier messed up the semicolon-less jQuery plugin, and if none of the situations I mentioned above was the case, then your minifier needs fixing, or you should start using a different one.
Joke aside, that's a good content put together in very clear and concise manner.
Oliver Hunt, primary JavascriptCore engineer took this quiz and said, "I rewrote our entire JS parser < 2 months ago, with all ASI rules and still didn't get 100% on a quiz"
Even if you include semicolons in all the “reasonable” places for them, it’s quite possible to get tripped up if you don’t understand what counts as a statement in JS and what doesn’t.
Edit to add: For example, don’t write code like this, with or without the autoinserted semicolons:
var x = function()
{
return (function(n){
return n
})(n)
}(function(){Math.exp(
f
)})
Someone else looking at the code is hardly going to be better off understanding it if you change that to: var x = function()
{
return (function(n){
return n;
})(n);
}(function(){Math.exp(
f
);});However, that’s not to say there is no reason to omit semicolons. I personally leave out semicolons as an extension of the DRY principle – if you are putting a line break after a statement anyway, then you might as well leave off the semicolon. It’s one less character to delete if you combine two adjacent lines into one. Also, if you already think of line breaks and statement ends going together, then semicolons are just noise that you have to make an effort to ignore – the effort required may be small, but there’s little reason not to avoid that effort.
The only problem with omitting semicolons in JavaScript is this case mentioned in “Semicolons in JavaScript are optional”:
a = b + c
(d + e).print()
The author recommends avoiding the error in that by changing the last line to ;(d + e).print()
. If the extra effort of adding a semicolon before all line-starting parentheses costs more effort than you would save by getting the two advantages I described above, then maybe adding semicolons everywhere is right for you. You might have to experiment with coding in both styles to be able to accurately quantify how much those two advantages and one disadvantage would affect you.The spec very clearly says they may be used, so I trust no one will have a problem with me using them. It's tough to drop that old C habit, anyway. Even my Python code has a random scattering of semicolons at the ends of lines.
...and I always feel a little guilty about it, because it seems wrong to add superfluous characters to my code. Is it actually "personal preference" and I've been feeling guilty for nothing, or is it better to leave off the semicolons?
As for whether you should omit the semicolons, only you can decide how much those pros and cons affect you. Depending on how much you care about others who read your code, you might also want to consider how you think those other people would choose, if they evaluated those pros and cons for themselves.
http://bonsaiden.github.com/JavaScript-Garden/#semicolon
(this really wasn't the example that I was thinking of, I'll scrounge for the other examples, sorry)
In other words, whether you write
return
foo;
or return
foo
the result is the same: an empty return, completely ignoring the foo bit.