Help regarding this javascript for loop

let primeNumber = 2;

nextPrime:
for(let i = 2; i <= primeNumber; i++) {
  for(let j = 2; j < i; j++) {
    if(i % j == 0) {continue nextPrime;}
  }

  alert(i);
  }

so, the output would be only 2, but my question is when the ‘continue’ gets executed, the loop gets iterated and the value for i goes from 2 to 3, but the alert function still outputs 2, whats exactly happening here?

The primeNumber is hardcoded because I want to know whats happening in this scenario.

Hi @Assad_Newar and welcome to the community :wave:

Your inner loop’s condition is false on the first condition (2 < 2 :arrow_right: false). Therefore it runs zero times and the continue never gets executed.

Does that make it clearer?

See you,
Michael

1 Like

Thanks, that cleared it up for me!

1 Like