i have scenario where an IF statement is inside FOR loop. i want to break the FOR loop inside IF statement , how to achieve it?
for(i=0;i<5;i++){
if(i==2){
break;
}
}
i have scenario where an IF statement is inside FOR loop. i want to break the FOR loop inside IF statement , how to achieve it?
for(i=0;i<5;i++){
if(i==2){
break;
}
}
Yes, you got it right.
See MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/break
And this StackOverflow question about breaking inside nested loops if you need it: Best way to break from nested loops in Javascript?
You can use label for this scenario along with continue and break keywords, as shown below It will skip when I is 1.
loop1:
for (var i = 0; i < 5; i++) {
if (i === 1) {
continue loop1;
}
}