JavaScript React Basics (retired) Thinking in Components Loops and Lists in JSX

Why does he include a comma at the end of every list item (array index) defined in JSX?

So he defines a PLAYERS variable where he stores player data. For Example

var PLAYERS = [ {name: "Name 1", score: 33}, {name: "Name 2", score: 40}, ]

My question is that why does he include a comma at the last object in the array?

Thomas BROMEHEAD
Thomas BROMEHEAD
3,091 Points

I don't believe it's necessary, have you tried running it w/o the comma?

2 Answers

Antti Lylander
Antti Lylander
9,669 Points

He does it because that ensures he will not forget to add it when he later adds another object in the array. This kind of habits will save you a lot of time in the long run.

Austin Johnson
Austin Johnson
11,447 Points

This is called the "trailing comma", and it is considered a best practice in programming languages that allow it such as Javascript and PHP. The logic is that if you want to add a new item to your object, you simply add a new line without needing to edit the previous line by adding a comma.

See the following link on the practice - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Trailing_commas