JavaScript and Semicolons

Did you realize that semicolons are optional in JavaScript? I have to admit ignorance until just a few weeks ago – and I’ve been programming JavaScript on and off for seven years. Naturally, when I learned this I went and stripped all the optional semicolons from my code (well, it seemed like a good way to kill a few minutes at the time). It was a bit jarring to see a curly brace language without semicolons. See for yourself1:

function(testName) start
  if (!this.log) return;
  this.testName = testName;
  this.lastLogLine = document.createElement('tr');
  this.statusCell = document.createElement('td');
  this.nameCell = document.createElement('td');
  this.messageCell = document.createElement('td');


function(testName) start
  if (!this.log) return
  this.testName = testName
  this.lastLogLine = document.createElement('tr')
  this.statusCell = document.createElement('td')
  this.nameCell = document.createElement('td')
  this.messageCell = document.createElement('td')

It also took a few days to get out of the semicolon habit – although I
still mistakenly put one in here and there (or more commonly name something
like_this instead of likeThis).

So is it worth it? I think so. I fall squarely in the camp that believes
syntax matters – the less of it the better. In fact, I think sparse syntax
plays a significant part in Python’s and Ruby’s popularity. Granted this change isn’t much – but
every little bit counts. Less syntax means less code which means faster

1Do you know which library this code comes from?
Its from unittest.js in

  1. Charlie Savage –
    July 23, 2007

    I know that folks may dislike semicolons, but there’s a good reason to put them in.

    Javascript’s semicolon insertion works by default as a response to a syntax error, namely, if the code generates a syntax error, then the compiler/interpreter tries replacing a nearby linefeed with a semicolon to see if the error goes away (see and the linked videos which are really worth watching)… semicolons may be optional, but you pay for them not being there.


  2. Charlie Savage –
    July 23, 2007

    Ah – very interesting, and thanks for the link.

    But I’m not sure that’s a compelling reason to use semicolons. We us [JavaScript Lint]( to verify out code and I think it catches those sorts of errors. I suspect [JsLint]( would also.

    Any opinion?