Open In App

How to use goto in Javascript ?

Last Updated : 23 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

There is no goto keyword in javascript. The reason being it provides a way to branch in an arbitrary and unstructured manner. This might make a goto statement hard to understand and maintain. But there are still other ways to get the desired result. The method for getting the goto result in JavaScript is the use of Break and Continue. In addition to its use in switch statements, the break statement can also be used to provide a “civilized” form of goto. By using this form of break you can break out of one or more blocks of code. These blocks need not be a part of some loop or switch, just any block of code. You can also precisely specify where the execution will resume because this form of break works with a label. So the conclusion is the break and continue is used to give you the benefits of goto without its drawbacks. 

The general syntax of labeled break is:

break label;

Similar is done for continue. Here label can be the name of the block of codes, it can be any variable but not a javascript keyword. 

Examples for Conversion:

var number = 0;

Start_Position
document.write("Anything you want to print");

number++;
if (number & lt; 100) goto start_position;

Note: This is not a code. Just an example where you want to use goto statement. 

Now this will be achieved in JavaScript as follows:

var number = 0;

start_position: while (true) {
    document.write("Anything you want to print");
    number++;

    if (number & lt; 100) continue start_position;
    break;
}

Here Continue and break both are used with a label to shift control to different parts of the program. It can be used in loops to pass controls to other parts, works well after checking certain conditions, and can be applied to many more logic statements. Now if we want to get out of the loop for a certain condition then we can use the break keyword. 

Take the above example and add a break into it for a certain condition.

var i;
for (i = 1; i & lt; = 10; i++) {
    document.write(i);
    if (i === 9) {
        break;
    }
}
document.write( & quot; < br > Learnt something new ");

Output:

123456789

Here we just used the break keyword to get out of the loop. 

Now again take the above example and add a continue statement.

var i;
for(i=1;i<=10;i++){
    if (i===4 || i===2) {
        continue;
    }
    document.write(i);
    if(i===6){
        break;
    }
}
document.write("
Learnt something new");

Output:

1356

Thus same outputs can be achieved with a break or continue and both are used to replace goto in JavaScript.

Example: 

HTML




<h2>JavaScript break</h2>
  
<p id="demo"></p>
  
<script>
    var cars = [
    "BMW", "Volvo", "Maruti", "Honda"];
    var text = "";
      
    list: {
        text += cars[0] + "<br>";
        text += cars[1] + "<br>";
        break list;
        text += cars[2] + "<br>";
        text += cars[3] + "<br>";
    }
      
    document.getElementById(
    "demo").innerHTML = text;
</script>


Output:

BMW
Volvo

Another program for Continue is given below. 

HTML




<h2>JavaScript Loops</h2>
  
<p>A loop with a <b>continue</b> statement.</p>
  
<p>A loop which will skip the step where i = 3.</p>
  
<p id="demo"></p>
  
<script>
    var text = "";
    var i;
    for (i = 0; i < 10; i++) {
        if (i === 3) {
            continue;
        }
        text += "The number is " + i + "<br>";
    }
    document.getElementById("demo").innerHTML = text;
</script>


Output

How to use goto in Javascript ?



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads