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

Ovidiu Dan
PLUS
Ovidiu Dan
Courses Plus Student 329 Points

Why do we use comas at the end of code-blocks?

Hi!

I noticed that when we are defining things like propTypes or defaultProps or arrays with objects, the instructor adds a coma at the end? Why is that?

Removing it produces absolutely no error and in my opinion is less confusing when re-reading the code so why does he opt to do this?

EX:

var PLAYERS = [
  {
    name: "Randy Orton",
    score: 31,
    id: 1
  },
  {
    name: "John Cena",
    score: 29,
    id: 2,
  },
  {
    name: "Alena Holligan",
    score: 35,
    id: 3,
  },
];
Application.propTypes = {
  title: React.PropTypes.string,
  players: React.PropTypes.arrayOf(React.PropTypes.shape({
    name: React.PropTypes.string.isRequired,
    score: React.PropTypes.number.isRequired,
    id: React.PropTypes.number.isRequired,
  })).isRequired,
};
Application.defaultProps = {
 title: "Scoreboard", 
}

Thank you in advance!

2 Answers

Tadeáš Firich
Tadeáš Firich
7,866 Points

Hello Ovidiu Dan, The reason is in this when you add some stuff behind the line where is no comma it will produces and error. Adding comma at the end is prevention for huge headache from the errors.

Was it helpful for you?

Ovidiu Dan
Ovidiu Dan
Courses Plus Student 329 Points

Thanks for your reply! So, in other words, this is preventing errors when you want for instance to add some more code at the bottom of your code-block? Kinda like taking an extra step just to prevent headaches? :)

Tadeáš Firich
Tadeáš Firich
7,866 Points

Yes, one extra click save 5 minutes of your life. :D