Break statement: The break statement is used to jump out of a loop. It can be used to “jump out” of a switch() statement. It breaks the loop and continues executing the code after the loop.
Example:
<!DOCTYPE html> < html > < head > < title > JavaScript Break statement </ title > </ head > < body style = "text-align:center;" > < div > < h1 >GeeksForGeeks</ h1 > < h2 >JavaScript Break</ h2 > </ div > < p id = "GFG" ></ p > < script > var content = ""; var i; for (i = 1; i < 1000 ; i++) { if (i === 6) { break; } content += "Geeks" + i + "<br>"; } document.getElementById("GFG").innerHTML = content; </ script > </ body > </ html > |
Output:
Continue statement: The continue statement “jumps over” one iteration in the loop. It breaks iteration in the loop and continues executing the next iteration in the loop.
Example:
<!DOCTYPE html> < html > < head > < title > JavaScript continue statement </ title > </ head > < body style = "text-align:center;" > < div > < h1 >GeeksForGeeks</ h1 > < h2 >JavaScript continue</ h2 > </ div > < p id = "GFG" ></ p > < script > var content = ""; var i; for (i = 1; i < 7 ; i++) { if (i === 4) { continue; } content += "Geeks" + i + "<br>"; } document.getElementById("GFG").innerHTML = content; </ script > </ center > </ body > </ html > |
Output:
JavaScript labels: In JavaScript, the label statements are written as statements with a label name and a colon.
Syntax:
- break statements: It is used to jump out of a loop or a switch without a label reference while with label reference, it used to jump out of any code block.
break labelname;
- continue statements: It used to skip one loop iteration with or without a label reference.
continue labelname;
Example: This example use break label statements.
<!DOCTYPE html> < html > < head > < title > JavaScript continue statement </ title > </ head > < body style = "text-align:center;" > < div > < h1 style = "color:green;" > GeeksForGeeks </ h1 > < h2 >JavaScript break</ h2 > </ div > < p id = "GFG" ></ p > <!-- Script to use break label --> < script > var val = ["Geeks1", "Geeks2", "Geeks3", "Geeks4", "Geeks5"]; var print = ""; breaklabel: { print += val[0] + "< br >" + val[1] + "< br >"; break breaklabel; print += val[2] + "< br >"+ val[3] + "< br >" + val[4]; } document.getElementById("GFG").innerHTML = print; </ script > </ body > </ html > |
Output:
Example: This example uses continue label.
<!DOCTYPE html> < html > < head > < title > JavaScript continue label </ title > </ head > < body style = "text-align:center" > < div > < h1 style = "color:green;" > GeeksForGeeks </ h1 > < h2 >JavaScript continue</ h2 > </ div > < p id = "GFG" ></ p > <!-- Script to use continue label --> < script > var val = ["Geeks1", "Geeks2", "Geeks3", "Geeks4"]; var val1=["Geeks","For","Geeks"] var print = ""; print += val1[0] + "< br >"; print += val1[1] + "< br >"; print += val1[2] + "< br >"; continuelabel: { print += val[0] + "< br >"; print += val[1] + "< br >"; continue continuelabel; print += val[2] + "< br >"; print += val[3] + "< br >"; } document.getElementById("GFG").innerHTML = print; </ script > </ body > </ html > |
Output:
Example: This example illustrates without using any label.
<!DOCTYPE html> < html > < head > < title > No label in JavaScript </ title > </ head > < body style = "text-align:center;" > < div > < h1 style = "color:green;" > GeeksForGeeks </ h1 > < h2 >JavaScript No label</ h2 > </ div > < p id = "GFG" ></ p > < script > var val = ["Geeks1", "Geeks2", "Geeks3", "Geeks4"]; var val1=["Geeks","For","Geeks"] var print = ""; labelloop:{ print += val1[0] + "< br >"; print += val1[1] + "< br >"; print += val1[2] + "< br >"; } print+="< br >"; labelloop1: { print += val[0] + "< br >"; print += val[1] + "< br >"; print += val[2] + "< br >"; print += val[3] + "< br >"; } document.getElementById("GFG").innerHTML = print; </ script > </ body > </ html > |
Output: