EDIT 2: The above question prompted me to scratch an itch that has bothered me for some time. After quite a bit of rumination, I have come up with a good justification for leading commas: easily spot missing commas. This is a potentially nasty source of bugs, since, rather than complain, the compiler will simply add a semicolon, thus changing the meaning quite dramatically:
var a = foo(),
b = bar() // Oops...
c = baz(); // Ouch!
The third line no longer initialises a new variable in the current scope. It assigns to an existing variable in an outer scope or initializes a new global variable if there isn't one.
This doesn't explain the use of leading commas in JSON. Perhaps it just carried over by fiat.
That said, I still favour separate declarations, which make it all simple and safe.
I'll leave my rant in place for posterity.
I agree that it appears all over the place. Almost every second library I peruse uses this convention. Frankly, I don't get it.
Trailing commas make it awkward to comment out the last line of a group...
var //a = foo(), easy
b = bar(),
c = baz();
var a = foo(),
b = bar()/*, awkward
c = baz()*/;
...whereas leading commas make it awkward for both the first and last line...
var /*a = foo() awkward
, */b = bar()
, c = baz();
var a = foo()
, b = bar()/* awkward
, c = baz()*/;
This is progress?
(One could of course win a cheap point by observing that semicolons are optional, but this only achieves parity at the expense of shunning best-practice.)
Worse, leading commas are also used for JSON. Here's a package.json generated by Express.
{
"name": "application-name"
, "version": "0.0.1"
, "private": true
, "dependencies": {
"express": "2.5.10"
, "jade" : ">= 0.0.1"
}
}
This is a PITA because most editors don't like indenting sibling lines differently to each other. Note that the first line of a group is indented more than the following lines.
The use of leading commas strikes me as a pointless affectation that causes problems without solving any. The only argument I can think of that has any merit is that leading commas line up with each other; not very compelling.
Fortunately, CoffeeScript is taking over the world, which renders the question moot (it even trounces JSON for config).
EDIT: I forgot to mention that I don't actually favour trailing commas for the above scenario. I think declaring individual vars makes the most sense. It's clean, regular and makes it very easy to comment out individual items.
var a = foo();
var b = bar();
var c = baz();