What Is The Point Of Using Labels In Javascript (label: Stuff Here) Outside Switches?
What is the point of using labels in javascript (label: stuff here) outside switches?
Solution 1:
You can use them as goto statements on break
and continue
, though admittedly you rarely see this in practice. You can find a few examples here.
Here's a quick one:
myLabel:
for(var i=0; i<10; i++) {
if(i==0) continue myLabel; //start over for some reason
}
Solution 2:
They're also useful in loops:
var x, y;
outer: for (x = 0; x < 10; ++x) {
for (y = 0; y < 10; ++y) {
if (checkSomething(x, y)) {
break outer;
}
}
}
...which breaks out of both loops if checkSomething
returns true.
I've never actually coded one of those, I've always split the inner loop off to a function or similar, but you can do it that way. Some people consider it bad style, akin to goto
(which JavaScript doesn't have).
Post a Comment for "What Is The Point Of Using Labels In Javascript (label: Stuff Here) Outside Switches?"