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 do you put commas after the last item in arrays and skip semi colons at the end of statements? Seems wrong.

As a Java developer, it just seems like bad form even if the code compiles to have commas after the last item in arrays and to skip semi colons at a clear end of a statement. Why is this done in your code?

2 Answers

Steven Parker
Steven Parker
229,744 Points

I agree with Jason, but I'd add that even though the language doesn't require semicolons at the end of a statement when nothing else is on the same line, it is considered a "best practice". This is because it makes it much less likely that errors will be introduced when the code is modified later. It can also make it easier for someone reading the code to distinguish between complete statements and expressions that take more than one line.

Another "best practice" is using nest-level indentation. While it's not required by the language (unlike Python), it's still considered a "best practice" as it makes reading and maintaining the code much easier.

I really appreciate how the examples in the Treehouse teachings are generally consistent about using the best practices for each language.

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

HI Ronald,

That is just how the creators designed JavaScript. Every language (and creator of a language) will have its own syntax, based on what they want to do.
Look at Ruby, for example. There are no commas, no semicolons, no curly braces, nothing. It's kind of like Python that way, except in Python Indentation and Whitespace is crucial and absolute, yet in almost every other language, neither of those matter.

There will obviously be some similar syntax rules across different languages, but usually there are many more differences.

Keep Coding! :) :dizzy: