January 2001
Beginner
312 pages
6h 4m
English
Going hand-in-hand with the continue statement is the break statement. Whereas the continue statement forces another iteration, the break statement jumps you out of the loop completely!
You typically use the break statement in while, for, for ... in, and do ... while statements (and switch statements, which we've not covered yet).
In the following example, the first alert box (displaying a 1) is shown, after which the break statement comes into force and kicks you out of the loop:
<script language="JavaScript">
<!-- Cloaking device on!
var x = 0;
while (x < 10)
{
x++;
alert(x);
break;
alert("Never seen!");
}
// Cloaking device off -->
</script>
Again, the best way to use the break statement is in conjunction with an if
Read now
Unlock full access