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 Refactor Arrow Function Expressions

brandon downs
brandon downs
11,577 Points

Practice Arrow Functions in JavaScript

https://teamtreehouse.com/library/refactor-arrow-function-expressions

in refactor.js, it was said that one of the functions was as simplified as it could get. the printToTen function however, aren't you also able to remove the curly braces from a for loop if there is only one line of code within the loop?

Doron Geyer
seal-mask
.a{fill-rule:evenodd;}techdegree
Doron Geyer
Full Stack JavaScript Techdegree Student 13,897 Points

I had the same thoughts about this, I also removed the curly braces since the for loop is essentially 1 line of code. Steven Parker , perhaps you could weigh on on this? Again apologies for tagging you in things let me know if youd prefer that I didnt, you just seem knowledgeable and active and us beginners really appreciate your input.

2 Answers

ludopuig
seal-mask
.a{fill-rule:evenodd;}techdegree
ludopuig
Full Stack JavaScript Techdegree Student 6,436 Points

Yes, JS allows to remove the curly braces but it is not a good practice, reason why the teacher didn't remove them, I think...

Steven Parker
Steven Parker
231,007 Points

I'll skip braces myself on one-line loops or conditionals, but only when all these concerns are met:

  • I'm reasonably certain more lines of code won't be needed in the loop/condition later
  • The statement is simple and compact enough that its purpose is easily understood
  • I indent the line in question to visually indicate that is the body of the loop or condition
  • OR I place the "body" statement on the same line as the loop or condition

I just recently posted an example of this style in an answer to this question.

And bear in mind this is a personal choice, I'm not suggesting this is or should be an industry-wide "best practice". When in doubt .. use braces.