Is there a legitimate usage of using labeled statements in javascript, or is this something that is not really used in production code but just in the spec? For example, I was having the hardest time figuring out how to jump to places in the code and had to use this sort of hack to get around some of the labeled-statement/jump requirements:
'use strict';
let for_loop_ran = false;
main: do {
console.log("Top!");
let arr = [1,2,3,4,5];
if (for_loop_ran) {
break;
} else {
for_loop_ran = true;
for (let elem of arr) {
console.log(elem);
if (elem === 5) continue main;
}
}
} while (true);
console.log("Bottom!");
What would be a more realistic usage of labeled statements (or rather it seems 'labeled loops') and using continue
/ break
to one of those? Or is that type of coding discouraged.
I suppose one option might be jumping to different levels of a loop -- a semi-contrived example might be:
let maxNameLength = 5
let frequency = {}
let couples = [['david', 'rosy'], ['dylan', 'dasha/daria']];
couples: for (let couple of couples) {
persons: for (let person of couple) {
let nameLength = 0;
letter: for (letter of person) {
if (letter in frequency) {
frequency[letter] ++;
} else {
frequency[letter] = 1;
}
if (++nameLength > maxNameLength) continue persons;
}
}
}
console.log(frequency);