admin管理员组文章数量:1332383
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
4 Answers
Reset to default 4The 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.
本文标签: loopsJavascript Break StatementStack Overflow
版权声明:本文标题:loops - Javascript Break Statement - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742294849a2448518.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论