The break statement provides you with a means of exiting a loop early. The break statement is commonly used to exit a loop when a value has reached or exceeded a set limit, or when searching through an array, when you've found the data for which you were searching.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site

The break Statement

The break statement provides you with a means of exiting a loop early. The break statement is commonly used to exit a loop when a value has reached or exceeded a set limit, or when searching through an array, when you've found the data for which you were searching.

The example below increments the variable x each time through the loop. Within the loop is an if statement that tests if x is greater than 8, and if so, executes a break statement.

var x = 0;
while (x < 12)
{
  x++;
  if(x > 8) break;
}
alert("loop exited when x = " + x);

The example below searches the day array for the day name "Wednesday", and, upon finding it, executes a break statement to end the loop.

var day = new Array("Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday","Sunday");
var index = 0;
while(index < day.length)
{
  if(day[index] == "Wednesday") break;
  index++;
}
alert("Found " + day[index]);

-  Remember if you code nested loops, a break statement in an inner loop will not cause program flow to also exit an outer loop.

var x = 0;
var y = 0;
while(y < 12)
{
  while (x < 3)
  {
    if(y > 3) break;
    x++;
  }
  y++;
}
alert("loop exited x = " + x + " y = " + y);

In the example above, the inner loop contains a break statement to cause it to exit when y is greater than 3, but it exits only the inner loop, the outer loop continues to increment y.

Web Design Sections
Java Script Quick Reference
Java Script Data Types
Java Script Reserved Words
Java Script Arithmetic Operators
Comparison Operators
Java Script Arrays
Java Script Character Encoding and Decoding
The if/else Structure
The switch/case Structure
The for Loop
The while Loop
The break Statement
The continue Statement
JavaScript Math Object
Round a Number
Determine Absolute Value
Determine Minimum and Maximum
Generating Random Numbers
Java Script Trigonometric Methods
Java Script Number Object
Format a Number as Currency
Java Script Strings
Compare Two Strings
Find a Character or a Substring Within a String
Include a Quote Character in a String
Include a Backslash Character in a String
Define Lines in a String
Use Escape to Replace Dangerous Characters
Convert a Number to a String
Convert a String to a Number
The Document Object Model (DOM)
Accessing Web Page Elements
Interactively Set Webpage Colors
Get Webpage File Date and File Size
Dueling Windows
Cookie Power Made Easy

[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2007 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269