I just want to bypass item 3, not break the loop.
for (var i = 1; i<= 9; i++) {
if (i==3) break;
console.log(i);
}
Was very unfortunate hunting an answer on Google because I don't really know the magic keywords/technical designation.
Expected result:
1
2
4
5
6
7
8
9
Update: Would like to do the same with a .forEach
for (var i = 1; i<= 9; i++) {
if (i===3) {
continue;
}
console.log(i);
}
forEach: fiddle
var numbers = [1, 2, 3, 4, 5];
numbers.forEach(function(number){
if(number === 3) {
return
}
console.log(number);
});
Use continue instead of break.
continue
skips the rest of the loop body and moves on to the next iteration.
for (var i = 1; i<= 9; i++) {
if (i==3) continue;
console.log(i);
}