Why care about trailing commas in Node.js?

It probably won't improve your runtime or anything like this, but you have a vantage using the trailing comma related to the version control.

If you do not use it, git will detected that one line was modified and another one was added. But if you use it, git will detected that only one line was added:

With using trailing comma:

enter image description here

Without using trailing comma:

enter image description here


Update: I've changed my position on this. I think trailing commas are great because you don't have to change two lines to add a property, and the diff shows only the added line.

Old and wrong:

I found a great reason to not use trailing commas, but it's not specific to node.js:

From Johan Sundström:

The benefit of this format is diff friendliness: adding or removing a property or array member almost never touches any other lines but the one where a change actually happened, whereas trailing-comma syntax almost always touches two lines to get the commas right.

Conversely, changed lines will never have anything unrelated on them, which helps scanning commits visually immensely.


No, there is no technical reason to do that.

However, I never put trailing comas just because I think it makes for cleaner code. Probably some also have the habit coming from web development where, like you mentioned, you need to be careful about those because of IE.

Edit: This answer made sense back in 2012, but today, with major browser support and tools like Babel for older browsers, I think trailing commas should be the default for everyone. The benefits are that it makes adding a new line easier, and the relevant Git diff is cleaner.