Trailing Comma or Leading Comma

7 Dec 2008

,


We all know that Internet Explorer is a pain in the backside. But the particular pain I wanted to write about is its pickiness about commas in Javascript. For example:

var cars = new Array(
   'Saab' ,
   'Volvo',
   'BMW',
);

If you’re used to programming in Php or Perl, that trailing comma is perfectly okay, and could even be considered good practice because it makes really easy to extend the array – just add a new line and leave another trailing comma. The only problem is – this trailing comma is not strictly legal in Javascript. However, just about every implementation out there will allow it except for (cue drum roll) Internet Explorer.

So I’ve taken to coding lists like this:

var cars = new Array(
   'Saab' ,
  ,'Volvo'
  ,'BMW'
);

I’ve taken to using the same style pretty much everywhere I have a comma-separated list of things – it’s particularly helpful in SQL server stored procedures.

insert into t_users (
   store_id
   , user_accesslevel
   , user_status
   , user_number
   , user_title
   , user_firstname
   , user_lastname
) values (
   @store_id
   , @user_accesslevel
   , @user_status
   , @user_number
   , @user_title
   , @user_firstname
   , @user_lastname
)