iterate in javascript

In JavaScript, there are multiple ways to perform iteration. One of the most common methods is using loops such as for and while loops.

For loop

The for loop is used when the number of iterations is known beforehand.

index.tsx
for (let i = 0; i < 10; i++) {
  console.log(i);
}
51 chars
4 lines

This loop will iterate 10 times and the output will be 0 to 9.

While loop

The while loop is used when the number of iterations is not known beforehand.

index.tsx
let i = 0;
while (i < 10) {
  console.log(i);
  i++;
}
55 chars
6 lines

This loop will iterate 10 times and the output will be 0 to 9.

You can also use do-while loop, Array.forEach(), for-of loop, and many other methods for iteration.

gistlibby LogSnag