块语句将零个或多个语句分组。在JavaScript以外的语言中,它称为复合语句。
这是语法-
{
//语句列表
}要将标签添加到块,请使用以下命令-
Identifier_for_label: {
StatementList
}让我们将其用于break语句。您可以尝试使用break语句运行以下代码以使用标签来控制流程
<html>
<body>
<script>
document.write("Entering the loop!<br /> ");
outerloop: // This is the label name
for (var i = 0; i < 5; i++) {
document.write("Outerloop: " + i + "<br />");
innerloop:
for (var j = 0; j < 5; j++) {
if (j > 3 ) break ; // Quit the innermost loop
if (i == 2) break innerloop; // Do the same thing
if (i == 4) break outerloop; // Quit the outer loop
document.write("Innerloop: " + j + " <br />");
}
}
document.write("Exiting the loop!<br /> ");
</script>
</body>
</html>