How can I implement a `javascript goto`-like behavior in my code?

I need to create a loop where execution jumps back to a specific label, similar to using goto, like in this example:

start:
alert("RINSE");
alert("LATHER");
repeat: goto start

Is there a way to achieve this in JavaScript?

I’ve been working with JavaScript for several years, and while there’s no native javascript goto like in older languages, you can get very close using loops.

Here’s a simple approach using a while loop with a condition:

function rinseLather() {
    let repeat = true;
    while (repeat) {
        alert("RINSE");
        alert("LATHER");
        
        // Ask user if they want to repeat
        repeat = confirm("Do you want to repeat?");
    }
}
rinseLather();

By using this loop, you effectively simulate a javascript goto by looping back to the top until the user decides to stop. It’s clean, readable, and leverages the control structures JavaScript is designed for.

Jumping in as someone who’s dealt with similar patterns, I’d add that if you specifically want the block to run at least once (which sounds like your rinse-lather-repeat case), a do...while loop is an even closer match to a javascript goto feel.

function rinseLather() {
    let repeat;
    do {
        alert("RINSE");
        alert("LATHER");
        
        // Ask user if they want to repeat
        repeat = confirm("Do you want to repeat?");
    } while (repeat);
}
rinseLather();

This guarantees the sequence runs at least once before checking whether to loop again — subtly improving the “jump back” effect you’re asking for when mimicking a javascript goto.

As someone who’s explored both functional and imperative patterns, I’d suggest pushing it a bit further: recursion can mimic a javascript goto by directly re-invoking the function at the point you want to jump back to.

function rinseLather() {
    alert("RINSE");
    alert("LATHER");

    // Ask if they want to repeat
    if (confirm("Do you want to repeat?")) {
        rinseLather();  // Recursive call to simulate jumping back
    }
}

rinseLather();

This recursive approach gives you a natural ‘jump’ back to the top, much like a javascript goto. Just be aware: deep recursion can hit stack limits, but for interactive loops like this, it’s usually safe — and sometimes even makes the intent clearer than a loop.