Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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,686 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