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 The Solution

Question about loops

Hi guys.

Just finished the practice loops workshop after giving JavaScript a break for a while.

What are the different use cases for while, doWhile and for loops if I can essentially do the same thing with each, as shown in the above mentioned workshop?

Thanks in advance Happy coding

Paul

1 Answer

Steven Parker
Steven Parker
229,981 Points

Each type of loop is best suited to certain situations:

  • for β€” when you need to create and maintain a counter or index
  • while β€” when a non-counter condition will control the iterations
  • do...while β€” when the code must be performed before the condition is tested

Thanks for the reply