How to use goto in javascript?

10k views Asked by At

i get reference from here "How can I use goto in Javascript?"

i understand the code as below

[lbl] first:
alert("Test")
goto first;

however. why the code below does not work for me

goto end;
alert("skipped line");
[lbl] end:

when I run the above command I will get an error like thisenter image description here

1

There are 1 answers

0
Cees Timmerman On

Labels are for loops and blocks.

Loop usage:

var allPass = true
top:
for(var i=0; i < items.length; ++i)
    for(var j=0; j < tests.length; ++j)
        if(!tests[j].pass(items[i])){
            allPass = false
            break top
        }

You can also use continue label.

Block usage:

foo: {
    console.log("face")
    break foo
    console.log("this will not be executed")
}
console.log("swap")

Non-strict, non-generator, function usage:

L: function F(){}