Skip to content

JSLint and the trailing comma of death

February 11, 2011

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.

About these ads
One Comment leave one →
  1. October 22, 2012 12:36 pm

    Why not post some code? These lint programs, node-jslint included are proving to be a real bitch to use.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 30 other followers

%d bloggers like this: