What types of loops exist in JavaScript?

Experience Level: Junior
Tags: JavaScript

Answer

In JavaScript, there are three types of loops:

  • for loop
  • while loop
  • do...while loop

Here's an example of how each of these loops could be used:

// for loop example
for (let i = 0; i < 10; i++) {
  console.log(i);
}

// while loop example
let j = 0;
while (j < 10) {
  console.log(j);
  j++;
}

// do...while loop example
let k = 0;
do {
  console.log(k);
  k++;
} while (k < 10);

Comments

No Comments Yet.
Be the first to tell us what you think.