reset a loop in javascript

To reset a loop in JavaScript, you can use the continue statement. The continue statement skips the current iteration of a loop and continues with the next iteration. This means that you can use it to restart a loop from the beginning.

Here is an example of how you can use the continue statement to reset a for loop:

index.tsx
for (let i = 0; i < 10; i++) {   // loop from 0 to 9

  if (i === 5) {      // if i is equal to 5
    continue;         // skip this iteration and continue with the next one
  }

  console.log(i);     // output the value of i
}
228 chars
9 lines

In this example, when i is equal to 5, the continue statement is executed, which skips the current iteration and continues with the next iteration. This effectively resets the loop, and it starts from 0 again. The output of this code is:

index.tsx
0
1
2
3
4
6
7
8
9
18 chars
10 lines

As you can see, the loop starts at 0 and ends at 9, but skips the iteration where i is 5.

gistlibby LogSnag