How to use goto in Javascript ?

11 Oct, 2019
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 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 labelled 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 break keyword.
Take the above example and add 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 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 break or continue and both are used to replace goto in JavaScript.Example:
<html>  <body>      <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>  </body>  </html>
Output:
BMW
Volvo
Another program for Continue is given below.
<html>  <body>      <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>  </body>  </html>
Output:
JavaScript-Misc
Picked
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
How to append HTML code to a div using JavaScript ?
Difference Between PUT and PATCH Request
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Installation of Node.js on Linux
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?