Javascript

JavaScript Fundamentals -- Looping in JavaScript

PRO

In this section, we'll cover the following:

  • for loops
  • for ... of loops
  • for ... in loops
  • do while loops
  • break
  • continue

Each of these loops has similarities to each other, but accomplish the loop in a different manner. The point is to repeat some portion of your code a certain number of times. You could repeat the code by copying and pasting it X number of times, but you're extremely limited to how many times that code would run and you have to manage all those pieces of code. Loops allow you to write the code once and use it any number of times. They're real convenient and you'll frequently see them as you work with JavaScript.

Any time you need to do some piece of code more than once, you should look at the possibility of putting it in a loop. They don't work for every situation, but they will work in many.

For a reference on what we'll talk about in this section, read this blog post.