4

I have just learnt that you can label for statements and break them in an inside loop

Wakanda crazy crap is this?

fyi here is an example:```js
outerLoop:for(let i = 0; i<5; i++){
for(let j=0; j<5; j++){
console.log(i*j);
if(i*j === 8) { break outerLoop;}
}
}
```
Why do we even set a label with a colon, why?

Comments
  • 2
    This is just about the only use for this, unless you're iterating matrices you definitely don't need a nested loop in a single function.
  • 0
    @Lor-inc There are a lot of other uses for nested loops, i.e. comparing two arrays element by element. Of course you can use Array.map() and whatnot, but those are usually just abstracting away the fact that some kind of a loop is still used internally.
  • 4
    What the fuck
    No
    Don't do that
  • 1
    @kamen Use a method. Don't write ambivalent code and rely on your sense of what needs to be explained, create tiny methods and ensure that variable lifespan is the shortest possible.
  • 1
    @PrivateGER Svelte does this. Well, not really. They use this feature that absolutely nobody knows about to mark their variables syntactically, and because it's valid Javascript it will work even if not using their compiler. There is a talk about it on YouTube. I found it pretty fascinating.
  • 3
    @Marl3x I know that, but why would you ever want to do *that*?
Add Comment