On semi-colons in js


How do you respond to somebody who's claiming something's bad, knowing all the arguments why something is considered good, and still tries to spread the badness about it? I don't know, actually. I didn't want to burn myself with that one either. I'm afraid it'll be a useless discussion.

Today I was pointed towards a blogpost about optional semi-colons in Javascript. Reading that post made my toes curl. This is one of those guys. He basically put down all the arguments for using semi-colons and threw them all out. I don't agree with some of the counter arguments he used, but that's besides the point.

Now by itself, I don't really care. I mean, he knows the potential pitfalls leaving out the semi-colons so if he ever faces such a problem, he'll only have himself to blame for it. But what if he participates in some OSS project like CommonJS? Or how will he program at work, in an environment where maintainability and coworkers should be one of your primary concerns? Will he suddenly use semi's now? I doubt it.

@jdalton also points out that Safari 2 doesn't even allow leaving out semi's after throw statements. This would be one of those browsers mislav deems forgotten, I guess :)

Now recently Inimino also wrote a piece touching this subject. His main argument was that of legibility. Semi-colons would only add to unnecessary overhead and distract from the actual code. While I can't really comment on that fact, I doubt it'll outweigh the potential problems programmers could encounter in Javascript when they get sloppy while not using semi-colons.

I don't really want to get deep into those problems. The specification outlines them pretty clearly and there are tons of posts about these cases anyways (google them!). So let me just mention the two most important ones:

- A return/break/continue/throw statement with the argument on the next line will actually not use the argument (but use undefined).

Code: (JS)

- Certain multi line snippets could be interpreted wrongly (because auto semi-colons are not _always_ inserted).

Code: (JS)
var x = y
This would be executed as a single line; var x = y(a||b).alert(); (and most likely cause a weird error you'll be searching for a long time).

Code: (JS)
Not executed. There is no return allowed between these two components. Of course this is not a serious problem, but if you encounter it you'll be in serious problems.

So while semi-colons are optional, in some cases they are actually not. Granted, these cases are rare, but they are there and they can really be a pain to debug. So I think it is the responsibility of those that know about these thing to protect those that are unaware of the issues. Especially in a professional environment or a public group project, enforce semi-colons.

Just don't get used using the semi-colon-less style "for personal projects". It's very hard to unlearn a style once you get used to it. Before you know it you'll (try to) get this style into some project. And then what? On the other hand, how much is really involved in putting the semi's where they belong? Helping the readibility, structure and minification of your source while preventing possible obscure hard-to-debug-even-harder-to-fix bugs on the way.

As for the specification? Read Ecmascript 5, paragraph 7.9 on Automatic Semicolon Insertion.