Loops are used in JavaScript to perform repeated task(s) based on conditions. Conditions typically return true or false when analysed. A loop will usually continue running until the defined condition returns false.

There are different kinds of loops supported by JavaScript, which typically are:

  1. for — loops through a block of code a number of times.
  2. for/in — loops through the properties of an object.
  3. for/of — loops through the values of an iterable object.
  4. while — loops through a block of code while a specified condition is true.
  5. do/while — loops through a block of code while a specified condition is true.

Although they all do similar things, some loops can be the better choice in specific situations. That depends on your use cases.

I will also discuss 2 extra looping methods that are typically used as well which are:

  1. forEach()
  2. map()

P.S. The variables are declared here with var keyword as it is the most generally well known and the first thing learned usually. You can substitute it with let or const where it fits.

Let’s begin the explanation!

#javascript

Multiple Ways to Create Loops in JavaScript
1.10 GEEK