I actually knew about that, but really, it's more of a curiosity than anything. Like in "see this weird trick in JavaScript!" "What?! Weiiiird..." Turns out that foo: 42 is a label followed by a number and not a key/value pair.
In JavaScript, especially in modern development, labels are basically useless.
Or is there by any chance a very useful way to use them?
The only decent use case is to break out of multiple loops. But then again, it's a very limited case where for some reason you have to use loops instead of a functional approach.
The only decent use case is to break out of multiple loops.
In PHP we have e.g. continue 2; (continue the parent loop), or break 3; (break the grandfather loop) for this purpose ;)
We have goto but there's basically no legitimate use for it. The docs page has a bit of an Easter egg agreeing with that :p php.net/manual/en/control-structur...
My favourite is Swift that has continue label or break label to refer to what to continue or break, but no accompanying goto which I think is an important safety decision.
gameLoop:whilesquare!=finalSquare{diceRoll+=1ifdiceRoll==7{diceRoll=1}switchsquare+diceRoll{casefinalSquare:// diceRoll will move us to the final square, so the game is overbreakgameLoopcaseletnewSquarewherenewSquare>finalSquare:// diceRoll will move us beyond the final square, so roll againcontinuegameLoopdefault:// this is a valid move, so find out its effectsquare+=diceRollsquare+=board[square]}}print("Game over!")
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I actually knew about that, but really, it's more of a curiosity than anything. Like in "see this weird trick in JavaScript!" "What?! Weiiiird..." Turns out that
foo: 42
is a label followed by a number and not a key/value pair.In JavaScript, especially in modern development, labels are basically useless.
The only decent use case is to break out of multiple loops. But then again, it's a very limited case where for some reason you have to use loops instead of a functional approach.
Fortunately they're not! 😂
In PHP we have e.g.
continue 2;
(continue the parent loop), orbreak 3;
(break the grandfather loop) for this purpose ;)We have
goto
but there's basically no legitimate use for it. The docs page has a bit of an Easter egg agreeing with that :p php.net/manual/en/control-structur...Uh. That's even worse. Now instead of a descriptive label, one has to count the nested loops to find out what the heck this statement affects.
I don't prefer it, just stating it's there ;-)
My favourite is Swift that has
continue label
orbreak label
to refer to what to continue or break, but no accompanyinggoto
which I think is an important safety decision.developer.apple.com/library/conten...