Search code examples
javascriptcoding-styleconventions

Trailing comma at the end of the last object field JS


Is there a hidden meaning in a case where the last object field is followed by a trailing comma? For example, webstorm by default formats code according to the first case. But for me it seems a little messy, for example, in webpack config files where there is an object with a lot of fields.

const object = {
  field: '123',
};

const object = {
  field: '123'
};

Solution

  • There is nothing special about it. A trailing comma is optional, but has no bearing on the code itself. It is just a style thing.

    Some people prefer to have the trailing comma because then if you add a new row, in your Git commits, the diff will only affect the new line added. Without the trailing comma, adding a new row will show two lines changing.

    Beyond that, there really isn't much of a difference.