如何在JavaScript中标记块?
块语句将零个或多个语句分组。在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>热门推荐
10 考试祝福语简短给对象
11 双十一祝福语大全简短
12 鼠年新年贺卡祝福语简短
13 生日祝福语舅舅 简短独特
14 同事调动的祝福语简短
15 生日祝福语简短而有深意
16 姥爷生日祝福语简短精辟
17 红包祝福语简短网名
18 生日祝福语简短蛋糕上