loops - Javascript Break Statement - Stack Overflow

If you use the break statement will it break all if...else...if else statements and all loops? If so ho

If you use the break statement will it break all if...else...if else statements and all loops? If so how can you break just a single if/loop.

while(test here){if({break;}don't break;)}

If you use the break statement will it break all if...else...if else statements and all loops? If so how can you break just a single if/loop.

while(test here){if({break;}don't break;)}
Share asked Nov 30, 2011 at 0:42 AurangeAurange 9632 gold badges10 silver badges24 bronze badges 1
  • developer.mozilla/en/JavaScript/Reference/Statements/break – Jared Farrish Commented Nov 30, 2011 at 0:51
Add a ment  | 

4 Answers 4

Reset to default 4

The break statement will only break out of the one loop in that you put it in. It doesn't affect if/else statements. If you want to break an outer loop you can use labels, as in this answer.

while(true){
    if(something) {
        break;
        console.log("this won't ever be executed");
    } else {
        console.log("still in loop");
    }
}

The break statement will break the loop and continue executing the code that follows after the loop (if any).

Check: http://www.w3schools./js/js_break.asp

break; will jump to the end of the deepest containing for, do, while or switch statement.

break label; will jump to the end of the labelled one.

if doesn't matter.

It's an error to break to a non-existant label, or if the relevant for, do, while, or switch is in a different function body.

I don't think there is a break for if statements. You'll have to restructure your if statement to only execute the code you need.

There is, however, a continue; which skips to the next iteration of the loop, while break; quits the loop entirely.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742294850a4416910.html

相关推荐

  • loops - Javascript Break Statement - Stack Overflow

    If you use the break statement will it break all if...else...if else statements and all loops? If so ho

    18小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信