Labels to Control the Flow in Javascript

Let us see how to use labels to control the flow in javascript. A label can be used with break and continue statements to control the flow more precisely. A label is simply an identifier followed by a colon (:) that is applied to a statement or a block of code.
Note − Line breaks are not allowed between the ‘continue’ or ‘break’ statement and its label name. Also, there should not be any other statement in between a label name and associated loop.

Example

The following example shows how to implement Label with a break and continue statement.

<!DOCTYPE html>
<html>
<body>

<h1>Using Labels to Control the Flow in Javascript</h1>

<p id="demo"></p>

<script>
var display = ["Welcome", "to", "easycode","one","WebApp"];
var text = "";

list:
    for(i=0;i<5;i++)
    {
    if(display[i]!="easycode")
    {
    text += display[i] + "<br>";    
    continue list; 
    }
    else
    break list;
    }

document.getElementById("demo").innerHTML = text;
</script>
</body>

<br/><br/>
<p>Powered by easycode.onewebapp.com</p>
</html>

 

Output

Labels to Control the Flow in Javascript