I have some code that I absolutely must implement using goto
. For example, I want to write a program like this:
start:
alert("RINSE");
alert("LATHER");
repeat: goto start
Is there a way to do that in Javascript?
I have some code that I absolutely must implement using goto
. For example, I want to write a program like this:
start:
alert("RINSE");
alert("LATHER");
repeat: goto start
Is there a way to do that in Javascript?
Another alternative way to achieve the same is to use the tail calls. But, we don’t have anything like that in JavaScript. So generally, the goto is accomplished in JS using the below two keywords. break and continue, reference: Goto Statement in JavaScript
Here is an example:
To achieve goto-like functionality while keeping the call stack clean, I am using this method:
Please note that this code is slow since the function calls are added to timeouts queue, which is evaluated later, in browser's update loop.
Please also note that you can pass arguments (using
setTimeout(func, 0, arg1, args...)
in browser newer than IE9, orsetTimeout(function(){func(arg1, args...)}, 0)
in older browsers.AFAIK, you shouldn't ever run into a case that requires this method unless you need to pause a non-parallelable loop in an environment without async/await support.
No. They did not include that in ECMAScript: