6

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;
   }
}
Sriram J
  • 174
  • 1
  • 1
  • 9

2 Answers2

2

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?

Rotem Tamir
  • 1,397
  • 2
  • 11
  • 26
0

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;
 }
}
Vivek Chaudhari
  • 1,930
  • 1
  • 14
  • 20