One of my colleagues likes to leave hanging commas when defining constant Javascript objects:

var myObj = { prop1: 'smth', prop2: 'smthElse', // <--- Висячая запятая! } 

He motivates this by the fact that when correcting such an object, the programmer will receive a more relevant output in the differential (1 line instead of two).

I really don't like this style, but for reasons it is also purely aesthetic:
1. JSLint on such swears
2. Non-support in old IE (8-). But under them, we still do not write.

The standard generally allows this.

Attention, the question: do you still know the reasons for which the hanging commas should / should not be used? For example, a positive / negative impact on optimizers or something like that.

  • As a plus, you can safely add / remove new fields of the object without having to follow the comma. - Vladimir Gamalyan
  • one
    It is also convenient for automatic code generation, it is not necessary to process the last line separately. - Vladimir Gamalyan
  • @VladimirGamalian, well, the first is so-so argument, any validator will burn on this much more than a hanging comma. - Duck Learns to Take Cover
  • 2
    I support your colleague in terms of ease of reading diffa. In addition, with hanging commas, blame immediately finds the original revision, and not the one in which the comma was added. - PashaPash
  • one
    @Grundy generator logic is somehow complicated for the variant without the last comma. On the account of the uselessness of comments, I do not agree, moreover, in my opinion, the generated code should also be properly formatted for readability. - Vladimir Gamalyan

1 answer 1

Strictly speaking: the standard does not prohibit the use of trailing commas in the object initializer

 ObjectLiteral [Yield]:
 {}
 {PropertyDefinitionList [? Yield] }
 {PropertyDefinitionList [? Yield] , }

Thus, to put or not to put a comma entirely on the conscience of the developer.

In the end, everything is decided by an agreement within the development team so that the code is monotonous.

Since this is a valid initialization variant, there should be no influence on optimization (minimization), but it depends on the implementation of the optimizer / minifier.

As for checkers: it all depends on the rules, which can be quite flexibly configured.


UPD: July 2017

I will add that, in July 2017, one of the most popular styled guides - the styled guide from airbnb does not just prohibit hanging commas, but requires them .

  • Since this is a valid initialization variant, there should be no influence on optimization (minimization). - The statement seems to me wrong. - Duck Learns to Take Cover
  • And the fact that the standard does not prohibit and the rules can be customized, so it’s understandable) - Duck Learns to Take Cover
  • @ru_volt, The statement seems to me to be wrong - why? there, by the way, there is a postscript, which may depend on the implementation :) besides, it’s more than sure that if not all, then most of the minifiers will simply drop this comma, as an important part - Grundy
  • one
    @ru_volt, well, this will be the problem of a particular optimizer / minifier - Grundy
  • 3
    @ru_volt, if your optimizer is not able to optimize the code, it is valid according to the standard - the price and the place in the garbage can hardly be seen. There are enough tools in the JS ecosystem that work as they should - Dmitriy Simushev