According to the ECMAScript standard, labels can be applied to all statements.
This means that the following lines are valid:
myLabel: throw 'whatever'
as well as
function foo() {
myLabel: return 'whatever'
}
console.log(foo())
But these labels appear to be unusable, which begs the question: why are they allowed? (Or maybe I have a fundamental misunderstanding)
According to Brendan Eich, the author of JavaScript, this approach aped Java (the new hotness in 1996), and avoided the need for "dead label analysis":
Same as Java, IIRC. We didn't want to require engines to do dead label analysis. I know for sure I didn't want to when I first implemented in SpiderMonkey. This has led to unintended transposed javascript: labels (used to be URL schemes), but they're harmless.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With