Learn JavaScript ES 2017 — Trailing Commas
Learn JavaScript ES 2017
If you had a list of chores, you might separate them with commas:
dishes, vacuum, tidy
A trailing comma is simply a comma that comes after the last item in our list. It’s not required, but in some instances in JS we’re allowed to put it there:
dishes, vacuum, tidy,
There are a number of reasons why you may want to include a trailing comma. Think about a scenario where you have to programmatically add elements to an array:
- Without trailing commas, most values can be added to the array as
value + ','
however you then need to add in special logic to ensure that last item added to the array doesn’t include a comma after it.