Take the 2-minute tour ×

I have some code that I absolutely must implement using goto. For example, I want to write a program like this:

repeat: goto start

Is there a way to do that in Javascript?

share|improve this question
add comment

10 Answers

up vote 30 down vote accepted

Absolutely! There is a project called Summer of Goto that allows you use Javascript at it's fullest potential and will revolutionize the way you can write your code.

This Javascript preprocessing tool allows you to create a label and then goto it using this syntax:

[lbl] <label-name>
goto <label-name>

For example, the example in the question can be written as follows:

[lbl] start:
[lbl] repeat: goto start;

You can see this example in action on jsFiddle, but be warned that it will be an infinite loop and you might have to forcefully shut down your browser to get out of it.

Note that you are not just limited to simple trivial programs like an endless LATHER RINSE repeat cycle--the possibilities afforded by goto are endless and you can even make a Hello, world! message to the Javascript console 538 times, like this:

var i = 0;
[lbl] start:
console.log("Hello, world!");
if(i < 538) goto start;

Again, you can see this in action on jsFiddle, and this time you don't have to worry about having to end the program forcefully.

You can read more about how goto is implemented, but basically, it does some Javascript preprocessing that takes advantage of the fact that you can simulate a goto with a labelled while loop. So, when you write the "Hello, world!" program above, it gets translated to something like this:

var i = 0;
start: while(true) {
  console.log("Hello, world!");
  if(i < 538) continue start;

There are some limitations to this preprocessing process, because while loops cannot stretch across multiple functions or blocks. That's not a big deal, though--I'm sure the benefits of being able to take advantage of goto in Javascript will absolutely overwhelm you.

share|improve this answer
Wow. Never thought I'd see the day. This is a joke, right? –  Surreal Dreams Mar 17 '12 at 15:35
@SurrealDreams It may be a joke, but it actually works. You can click the jsFiddle links and see that they actually work. –  Peter Olson Mar 17 '12 at 15:36
The article you linked to actually states it's a joke :) –  pimvdb Mar 17 '12 at 15:39
Watch out for those Velociraptors :) –  Surreal Dreams Mar 17 '12 at 15:42
@GoldenNewby Some people learn a great deal about programming by being playful with languages and tools. It is also often a more enjoyable way to learn. –  Daniel Mar 17 '12 at 19:20
show 10 more comments

No. They did not include that in ECMAScript:

ECMAScript has no goto statement.

share|improve this answer
I was wondering if GOTO would be useful while debugging JavaScript. Afaik, only IE provides GOTO in its debugger... and I actually found a use-case for it, but I'm not sure if it could be useful generally... to jump around while debugging JavaScript. What do you think? –  Šime Vidas Apr 30 '12 at 20:41
@Šime Vidas: I'm not sure whether debugging with goto functionality is useful. Basically you'd be messing around with the code path in a way that would never happen without debugging anyway. –  pimvdb May 1 '12 at 7:44
What a pity ... IMHO goto would fit just perfectly well into javascript's cocktail of stupid "features" :) –  Yura Apr 9 '13 at 15:35
goto is a reserved keyword for future use, however. We can only hope :) –  Azmisov Aug 2 '13 at 19:18
add comment

Actually, I see that ECMAScript (JavaScript) DOES INDEED have a goto statement. However, the JavaScript goto has two flavors!

The two JavaScript flavors of goto are called labeled continue and labeled break. There is no keyword "goto" in JavaScript. The goto is accomplished in JavaScript using the break and continue keywords.

And this is more or less explicitly stated on the w3schools website here http://www.w3schools.com/js/js_switch.asp.

I find the documentation of the labeled continue and labeled break somewhat awkwardly expressed.

The difference between the labeled continue and labeled break is where they may be used. The labeled continue can only be used inside a while loop. See w3schools for some more information.


Another approach that will work is to have a giant while statement with a giant switch statement inside:

while (true)
    switch (goto_variable)
        case 1:
            // some code
            goto_variable = 2
        case 2:
            goto_variable = 5   // case in etc. below
        case 3:
            goto_variable = 1

         etc. ...

share|improve this answer
add comment

How about a for loop? Repeat as many times as you like. Or a while loop, repeat until a condition is met. There are control structures that will let you repeat code. I remember GOTO in Basic... it made such bad code! Modern programming languages give you better options that you can actually maintain.

share|improve this answer
add comment

In classic JavaScript you need to use do-while loops to achieve this type of code. I presume you are maybe generating code for some other thing.

The way to do it, like for backending bytecode to JavaScript is to wrap every label target in a "labelled" do-while.

LABEL1: do {
  x = x + 2;
  if (x < 100) break LABEL1;
  if (x < 100) continue LABEL1;
} while(0);

Every labelled do-while loop you use like this actually creates the two label points for the one label. One at the the top and one at the end of the loop. Jumping back uses continue and jumping forwards uses break.


  x = x + 1;
  if (x > 100) goto DONE_LOOP;

MYLOOP: do {
  x = x + 1;
  if (x > 100) break MYLOOP;
  continue MYLOOP;// Not necessary since you can just put do {} while (1) but it     illustrates
} while (0)

Unfortunately there is no other way to do it.

Normal Example Code: while (x < 10 && Ok) { z = 0; while (z < 10) { if (!DoStuff()) { Ok = FALSE; break; } z++; } x++; }

So say the code gets encoded to bytecodes so now you must put the bytecodes into JavaScript to simulate your backend for some purpose.

JavaScript style: LOOP1: do { if (x >= 10) break LOOP1; if (!Ok) break LOOP1; z = 0; LOOP2: do { if (z >= 10) break LOOP2; if (!DoStuff()) { Ok = FALSE; break LOOP2; } z++; } while (1);// Note While (1) I can just skip saying continue LOOP2! x++; continue LOOP1;// Again can skip this line and just say do {} while (1) } while(0)

So using this technique does the job fine for simple purposes. Other than that not much else you can do.

For normal Javacript you should not need to use goto ever, so you should probably avoid this technique here unless you are specificaly translating other style code to run on JavaScript. I assume that is how they get the Linux kernel to boot in JavaScript for example.

NOTE! This is all naive explanation. For proper Js backend of bytecodes also consider examining the loops before outputting the code. Many simple while loops can be detected as such and then you can rather use loops instead of goto.

share|improve this answer
+1 love the idea not having a condition –  ChaosClown Jan 18 at 0:06
add comment
    start = 0,
    more = 1,
    pass = 2,
    loop = 3,
    skip = 4,
    done = 5;

var label = start;

while (true){
    var goto = null;
    switch (label){
        case start:
        case more:
        case pass:
        case loop:
            goto = pass; break;
        case skip:
        case done:

    if (goto == null) break;
    label = goto;
share|improve this answer
add comment

You should probably read some JS tutorials like this one.

Not sure if goto exists in JS at all, but, either way, it encourages bad coding style and should be avoided.

You could do:

while ( some_condition ){
share|improve this answer
add comment

Generally, I'd prefer not using GoTo for bad readability. To me, it's a bad excuse for programming simple iterative functions instead of having to program recursive functions, or even better (if things like a Stack Overflow is feared), their true iterative alternatives (which may sometimes be complex).

Something like this would do:

while(true) {

That right there is an infinite loop. The expression ("true") inside the parantheses of the while clause is what the Javascript engine will check for - and if the expression is true, it'll keep the loop running. Writing "true" here always evaluates to true, hence an infinite loop.

A similar example (to explain these conditions and the while clause), the following would be the same:

while(2 + 2 == 4) {

One could (however) argue why you need an infinite loop of alert boxes. I hope you're using this for something good, not for something bad.

share|improve this answer
add comment

You can simple use a function:

function hello() {
share|improve this answer
This is a really bad idea as it will keep pushing the return address on the call stack until the system runs out of memory. –  Paul Hutchinson Mar 19 at 21:26
add comment

There is a way this can be done, but it needs to be planned carefully. Take for example the following QBASIC program:

1 A = 1; B = 10;
10 print "A = ",A;
20 IF (A < B) THEN A = A + 1; GOTO 10
30 PRINT "That's the end."

Then create your JavaScript to initialize all variables first, followed by making an initial function call to start the ball rolling (we execute this initial function call at the end), and set up functions for every set of lines that you know will be executed in the one unit.

Follow this with the initial function call...

var a, b;
function fa(){
    a = 1;
    b = 10;
function fb(){
    document.write("a = "+ a + "<br>");
function fc(){
    document.write("That's the end.<br>");

The result in this instance is:

a = 1
a = 2
a = 3
a = 4
a = 5
a = 6
a = 7
a = 8
a = 9
a = 10
That's the end.
share|improve this answer
add comment

Your Answer


