Skip to content

Conversation

@lumosmind
Copy link
Contributor

@lumosmind lumosmind commented Feb 12, 2021

Changed line make me think "we can only use label and break/continue with loops". But this is not totally true. We can use label and break with simple JavaScript blocks and function declarations. So, I tried to make it more clear.

MDN

Co-authored-by: Muhammed Zakir <8190126+MuhammedZakir@users.noreply.github.com>
@iliakan
Copy link
Member

iliakan commented Mar 12, 2021

@joaquinelio Wow, I never ever saw break used this way.

Maybe it's worth a better explanation?

@iliakan
Copy link
Member

iliakan commented Mar 12, 2021

Imho, that looks really weird.

If break is allowed to work with blocks, then why non-labelled break doesn't work?

This works:

foo: {
  console.log('face');
  break foo;
  console.log('this will not be executed');
}
console.log('swap');

...But this doesn't (no label):

{
  console.log('face');
  break;
  console.log('this will not be executed');
}
console.log('swap');

@lumosmind
Copy link
Contributor Author

Imho, that looks really weird.

If break is allowed to work with blocks, then why non-labelled break doesn't work?

This works:

foo: {
  console.log('face');
  break foo;
  console.log('this will not be executed');
}
console.log('swap');

...But this doesn't (no label):

{
  console.log('face');
  break;
  console.log('this will not be executed');
}
console.log('swap');

You are right that's my mistake but it does not effect the changed line. functions and blocks must be labeled when they contain break keyword. I just couldn't write good explanation as MDN.

@iliakan
Copy link
Member

iliakan commented Mar 13, 2021

Thanks for the note, I changed some phrases to reflect that.

@iliakan iliakan closed this in a89de35 Mar 13, 2021
@iliakan
Copy link
Member

iliakan commented Mar 13, 2021

...Without going much in-detail of that really weird feature =)

@lumosmind
Copy link
Contributor Author

...Without going much in-detail of that really weird feature =)

yes really weird :) it feels like goto keyword.

@joaquinelio
Copy link
Member

I guess more a side effect than a "feature"
they didn't bother

@joaquinelio
Copy link
Member

joaquinelio commented Mar 15, 2021

it's not weird, it is beautiful.
Classical break feels like goto.
The concept is cleaner and universal: You label ANY block you are breaking out. (*)
(the for is a block without brackets)

we can forget the "goto but no goto" thing - and its warnings.

...
(*) Is grammar ok?
break jail, like the tool inside the cake

@joaquinelio
Copy link
Member

not weird but cool

invoice: {

    for invoiceItems {

        processItem 
        if inPain   break invoice     // truly
    } 
    processPayment     // can break invoice here too
    printing 
    etc

} 

pretty clean

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants