Hosting Solutions
mobile_logo Table of Contents
seal_frontseal_back

break

The break statement is used to terminate a loop, switch or label statement from further processing. Apply it when you want to force one of those types of statements to stop processing. This statement also works with the label statement.

Syntaxbreak optional_label;

terminating a loop statement

for (i = 0; i < 20; i++) { if(i >= 5){ break; } document.write("Pass index "+i+" of the loop<br>"); } Using break above we were able to take a loop that has a ceiling of 20 passes, and force it to stop processing after 5 passes.

terminating a switch statement

var browser = "Chrome"; switch (browser) { case "IE": document.write("Internet Explorer makes life hard for webmasters."); break; case "Chrome": document.write("Chrome conforms to popular browser standards."); break; case "Firefox": document.write("Firefox is bulky but developer friendly."); break; default: document.write("No information for browser: "+browser); } If break is not applied to cases of a switch statement, the switch statement will keep processing until its end.

JavaScript

Statements

breakconstcontinuedo whileforfor inif elselabelreturnswitchtry catchvarwhilewith