Skip to content
Advertisement

Is using labels in JavaScript bad practice?

I just found out about using label s in JavaScript, such as:

for (var i in team) {
    if(i === "something") {
        break doThis: //Goto the label
    } else {
        doThat();
    }
}

doThis: //Label
doIt();

I’ve not heard about this until now and I can’t find much information online about it and I’m beginning to think there is a reason for that.

It seems to me like this is similar to a GOTO statement in other languages and would be considered bad practice. Would I be right in assuming this?

Advertisement

Answer

Those are loop breaker identifiers. They are useful if you have nested loops (loops inside loops) and using these identifiers, you can conditionally specify when and which loop to break out from.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement