-3

I had a problem which was that I was using a loop e.g

for(let i=0; i<10; i++){
if(i === 3){
    // go to the next iteration of the loop
}
console.log(i)

}

and I was struggling to see how to get to the next iteration. I tried a "return" statement but this came up with the error "illegal return statement" and having done a quick search on the forums the answer was not obvious, so I thought I'd log it here so that next time I can find it easier.

Matthew Player
  • 318
  • 4
  • 17
  • 1
    I recommend to bookmark this instead: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration#continue_statement –  Dec 03 '19 at 09:23

3 Answers3

0

Having looked through the MDN docs, what IU actually wanted was a "continue" statement which short-circuits the rest of the code in the block and goes straight to the next iteration.

Matthew Player
  • 318
  • 4
  • 17
0

You just have to write a continue keyword in for loop to goto next loop,

for(let i=0; i<10; i++){
    if(i === 3){
        continue;
    }
    console.log(i)
}

Note: choose let judiciously, it can also be used with var as it is having a function scope.

Ankur Soni
  • 5,725
  • 5
  • 50
  • 81
0

If you want to skip the rest of code in current iteration and get to next one, continue; https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/continue

If you want to exit loop completely, use break;

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of

matek997
  • 349
  • 1
  • 12