Grepular

JavaScript Comma Quirk

Written 14 years ago by Mike Cardwell

I keep tripping up over the same JavaScript quirk again and again. I once read somewhere that it was good practice in Perl arrays/hashes to stick a comma after the last item. For example, rather than:

my $foo = {
  bar => 'wibble'
};

You would write this:

my $foo = {
  bar => 'wibble',
};

The logic behind it is so that you don’t accidently forget the comma when adding new items. I’m not going to argue about whether or not this is a good idea. It’s just something I’ve become use to doing without thinking about it.

The trouble is, I do most of my web development in Firefox, where this JavaScript is valid:

var foo = {
  bar: 'wibble',
};

As soon as I load it up in Internet Explorer to test though, it fails. Internet Explorer is not at all happy about that comma at the end. This is by far the most frequent bug found in my JavaScript. It’s annoyingly difficult to train yourself out of a coding practice which you’ve been following for years.

Want to leave a tip?BitcoinMoneroZcashPaypalYou can follow this Blog using RSS. To read more, visit my blog index.