The JavaScript-break statement is used to exit the loop.
The break statement is used to exit a loop.
Continue is used to skip an iteration in a loop.
Break statement
We have seen the break statement. It is used to jump out of the switch () statement.
The break statement can be used to exit a loop.
After the break statement jumps out of the loop, it will continue to execute the code after the loop (if any ):
Instance
For (I = 0; I <10; I ++)
{
If (I = 3)
{
Break;
}
X = x + "The number is" + I +"
";
}
Try it yourself
Since this if statement has only one line of code, curly braces can be omitted:
For (I = 0; I <10; I ++)
{
If (I = 3) break;
X = x + "The number is" + I +"
";
}
Continue statement
The continue statement interrupts the iteration in the loop. If a specified condition exists, it continues the next iteration in the loop.
In this example, the value 3 is skipped:
Instance
For (I = 0; I <= 10; I ++)
{
If (I = 3) continue;
X = x + "The number is" + I +"
";
}
JavaScript tag
As you can see in the switch statement chapter, you can mark JavaScript statements.
To mark a JavaScript statement, add a colon Before the statement:
Label:
Statement
The break and continue statements are only statements that can jump out of the code block.
Syntax
Break labelname;
Continue labelname;
The continue Statement (with or without tag reference) can only be used in a loop.
The break statement (without tag reference) can only be used in loops or switches.
Through tag reference, the break statement can be used to jump out of any JavaScript code block:
Instance
Cars = ["BMW", "Volvo", "Saab", "Ford"];
List:
{
Document. write (cars [0] +"
");
Document. write (cars [1] +"
");
Document. write (cars [2] +"
");
Break list;
Document. write (cars [3] +"
");
Document. write (cars [4] +"
");
Document. write (cars [5] +"
");
}
++
<Script>
Cars = ["BMW", "Volvo", "Saab", "Ford"];
List:
{
Document. write (cars [0] +"
");
Document. write (cars [1] +"
");
Document. write (cars [2] +"
");
Break list;
Document. write (cars [3] +"
");
Document. write (cars [4] +"
");
Document. write (cars [5] +"
");
}
</Script>