JSLint and the trailing comma of death

I’m willing to bet that every seasoned web developer has, at one point or other, encountered the trailing comma of death. Without a doubt, it is one of (if not) the most common javascript errors to plague browserdom (actually, just Internet Explorer). And it reared its ugly head again this week at work.

A few months ago, I added JSLint to our continuous integration build with the express purpose of nipping such issues in the bud. Inexplicably, the trailing comma made it through.

What’s the deal, JSLint? You complain about aesthetic minutiae like mixed spaces and tabs, but not veritable browser issues like the trailing comma?

After digging into JSLint’s source, I found my answer on line 3967:

if (nexttoken.id === ']' && !option.es5) {

Of course! Being standards-supporting developers, we had enabled the “Tolerate ES5 syntax” option in our build. Good intentioned as it may have been, enabling that JSLint option allows syntax that causes IE to choke, manifesting as “bugs” in our software. Alas.

Why does ECMAScript 5 permit the trailing comma (see 11.1.4)? Probably because any programming language worth its salt does (eg, Java, Python). Trailing commas are, in fact, considered by many to be good practice. Consider this array:

var lost = [

If the Valenzetti Equation were to change, you can rearrange that array initialization any way you like without needing to fiddle with commas. Unless you’re using IE, of course.

One thought on “JSLint and the trailing comma of death

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s