7️⃣

4.7 Choosing A Loop

4.7 Choosing A Loop

Wow! There are a lot of loops in JavaScript. Which one do I use?
When it comes to choosing a loop, it all depends on the situation. So, the best way to know what kind of loop is needed takes practice.

General Guidelines

  • If you know the number of iterations you need to make, a for loop may be a good choice. Otherwise, a while loop may be better.
  • If you want to go through all properties of an object, use a for in loop.
  • If you want to go through everything in an iterable, use a for of loop.
  • If you want to run some piece of code once and you don't know the number of times the loop should be repeated, use a do while loop.
 
⚖️
Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.