JavaScript: The Definitive Guide

Previous Chapter 5
Statements
Next
 

5.7 break

The break statement has a very simple syntax:

break;

This statement is valid only within the body of a while, for, or for/in loop. Using it outside of a loop is a syntax error. When executed, the break statement exits the currently running loop. This statement is usually used to exit a loop prematurely when, for whatever reason (perhaps when an error condition arises), there is no longer any need to complete the loop. The following example searches the elements of an array for a particular value. If the value is found, a break statement terminates the loop:

for(i = 0; i < a.length; i++) {
    if (a[i] == target) 
        break;
}


Previous Home Next
for...in Book Index continue

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell
Hosted by uCoz