In most Node.js libraries people take special care removing trailing commas after the last key-property pair of objects:
var test = {
key1: 123,
key2: 456,
key3: 789
};
This produces some troubles while editing the code, e.g. to swap last two key-value pairs one has also to add one comma and to remove one. Some people move commas to the next line, which solves the issue with the last element but also makes the code a bit harder to read (IMHO):
var test = {
key1: 123
, key2: 456
, key3: 789
};
On the other hand as far as I know the trailing commas in JavaScript produce troubles only in some IE browsers. So I’m wondering are there any technical reasons not to write hashes with trailing commas in Node.js? (Like the following:)
var test = {
key1: 123,
key2: 456,
key3: 789,
};
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.