0

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.

3
  • 1
    break is a keyword in JavaScript. what are you trying to do? Commented Sep 9, 2013 at 6:11
  • You shouldn't be using break with if-statements Commented Sep 9, 2013 at 6:15
  • Indeed, what is your goal? Commented Sep 9, 2013 at 6:24

1 Answer 1

2

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');

Sign up to request clarification or add additional context in comments.

2 Comments

Ok...but cant we go to LableName_1 directly from he second if statement...is there any way like goto(I know that javascript has no goto statement)..but is there any other way like goto etc so that we can directly jump to LableName_1 ?
By using a function call. They are basically gotos, in a sense. I'd help you more if I knew what you were trying to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.