user2702482 user2702482 - 1 month ago 8
Javascript Question

javascript break label showing syntax error

I have the code below :

LableName_1:
if(ttipPos == 'left') {
//do sumthing
break L;
}
LableName_2:
if(ttipPos == 'right') {
//do sumthing
break LableName_1;
}


where I used
break
with label name

This is showing syntax error, but if I replace
break LableName_1
with
break LableName_2
within second
if
statement, the syntax error doesn't occur .... Is there any problem related to scope of using
break
with label.

Answer

The second break fails because it isn't inside of label #1. It can't break something it's not in.

LabelName_2: {
  LabelName_1: {
    while (1) {
      console.log('label 1');
      break LabelName_1;
    }
  }
  console.log('label 2')
  break LabelName_2;
}
console.log('out of labels');

Comments