Should you use semicolons in JavaScript?

There has been a quite a bit of chatter recently on whether or not you should use semicolons in your JavaScript code.

JavaScript provides a feature called Automatic Semicolon Insertion (ASI). For the most part there are rarely problems in omitting semicolons, but there are a few cases where semicolons are required to prevent syntax errors or resolve code ambiguities.

A site that triggered a lot of chatter was aresemicolonsnecessaryinjavascript.com which boldly states ‘NO’. The author is really trying to explain we should not just include semicolons everywhere due a fear that our code might break in some environments or parsers (such as JavaScript minifiers). Others are also continuing the same point that we should better understand why semicolons in JavaScript are optional.

Douglas Crockford, a leading expert in JavaScript recommends using semicolons at the end of every simple statement and his ubiqitious JavaScript code quality tool, JSLint, by default expects semicolons to be inserted. A poll on Stack Overflow strongly recommends using semicolons everywhere.

Lastly, I found Armin Ronacher’s post on dealing with JavaScript’s automatic semicolon insertion to be most useful. In particular his summary where he writes:

Are Semicolons Necessary in Javascript? Despite popular belief the answer is "sometimes" and not "no". But to save yourself time and troubles, just place them all the time. Not only will it save yourself some headaches, your code will also look more consistent. Because there will be situations where a semicolon becomes necessary to resolve ambiguities.

So what will I be doing? My vote goes to following Douglas Crockford’s JavaScript coding guidelines for now and I’ll be happily including semicolons in my code.

If you are interested, there are also a couple of discussions on Reddit regarding these couple of posts; Dealing with JavaScript’s automatic semicolon insertion and Are semicolons necessary in JavaScript?.

This entry was posted in JavaScript and tagged , , , . Bookmark the permalink. Both comments and trackbacks are currently closed.

6 Comments

  1. Posted February 10, 2011 at 4:54 am | Permalink

    Just recently finished reading the Google JavaScript Style Guide (http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml) and they say ‘yes’ to semicolons. They even give some examples of when not using them can cause serious issues. (http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml#Semicolons)

    I use them, and always have. Originally simply for the aesthetics of it, now for good reason.

    Enjoyed your article!

  2. Posted February 10, 2011 at 3:34 pm | Permalink

    I do some Blackberry development. A customer, who developed an intranet website noticed Blackberry users were having difficulties using even his mobile-specific version. Turns out, he never used semicolons in his Javascript, and the BB browser apparently likes them. a lot. This guy is also a FoxPro guru, and apparently the semicolon is *line continuation* character in that programming environment, which probably explains why he didn’t use them in Javascript. Although I can’t imagine how he learned Javascript and didn’t now about semicolons – still trying to figure that one out.

  3. Posted February 10, 2011 at 3:43 pm | Permalink

    I am personally a fan of semi-colons.

  4. Posted February 10, 2011 at 6:18 pm | Permalink

    I personally am also a fan of semicolons, whitespace in fact anything that helps another developer, including myself, understand \ interpret the code easliy.

  5. Posted February 10, 2011 at 7:28 pm | Permalink

    Always have been a fan of the semicolon. I looked into this a while back and the recommendations of Google in particular made me decide to always use them.

  6. serg
    Posted June 7, 2011 at 5:17 pm | Permalink

    Пишите еще!