Use return value of that function and call continue based on it:
for (var i = 0; i < 5; i++) {
if (theFunction(i)) {
continue;
}
}
function theFunction(x){
if (x == 3) {
return true;
}
return false;
}
Btw in your code you have if(i=3){, be aware that you need to use == or ===, single equals sign is for assignment.
Use return value of that function and call continue based on it:
for (var i = 0; i < 5; i++) {
if (theFunction(i)) {
continue;
}
}
function theFunction(x){
if (x == 3) {
return true;
}
return false;
}
Btw in your code you have if(i=3){, be aware that you need to use == or ===, single equals sign is for assignment.
My solution would be, if putting for loop inside the function is not a big deal
function theFunction(initial, limit, jump) {
for (var i = initial; i < limit ; i++) {
if (i == jump) {
continue;
} else {
console.log(i)
}
}
}
Videos
What is the difference between break and continue in JavaScript?
Can I use continue inside a forEach callback?
Why does continue cause an infinite loop in my while loop?
Putting in an else block would make the code functionally equivalent, yes. As for whether it's better? Like others have said, the JS interpreter won't care - it'll handle it the same way regardless. The only important thing then is human readability.
If the internal for loop is long and complicated and contains lots of other stuff, then using continue may well be a better option, as it clearly states to the reader that it doesn't matter what else is in the loop, it'll go to the next iteration. using an if/else means the user has to confirm that there is no code further down that will execute after the if/else.
In another situation, using if/else might be better because using continue might end up in code duplication, or later maintenance might add a couple of lines to the end of the loop, thinking that every iteration will pass through it (always make your code as idiot-proof as possible if someone else may be modifying it later!).
Ultimately it comes down to opinion and coding standards (if it's part of a larger project then it should match what is elsewhere as much as possible). Do whatever makes the code most understandable to you. Ask someone else to have a look at the end product if you can, see if they have a different opinion to you - it's always easier to read your own code, having someone else look at it can highlight things that aren't quite as clear as you think they are!
How zerkms noticed, js interpreter will run this code similarly.
In my opinion, code with if..else statement becomes more readable, and also continue statement could be used, when there is nothing to do at this step of loop.