top of page
  • Writer's pictureDavid

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 = [
  4,
  8,
  15,
  16,
  23,
  42,
]

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.

Related Posts

See All

Ruby scripts for RPG Maker XP now on Github

Back in 2005 and 2006, while finishing up my CS degree, I taught myself Ruby and started writing extensions to RPG Maker XP with the aim of creating my own RPG. As it turns out, there’s still quite a

Rest in peace, IE6… now what?

Like many web shops of late, my workplace has officially discontinued support for Internet Explorer 6. My initial reaction was: Yay! No more PNG transparency issues! And I can finally use all those po

bottom of page