Abhinav rawat
Abhinav rawat

Reputation: 111

break not working with label inside a function error coming label not found

//break not working with label inside a function error coming label not found

var a = 10 ;
function myfn(){
  if(a===15){
    break stoplabel;
  }
  else{
    console.log(a);
    a++;
    stoplabel:myfn();
  }
}
myfn();

Upvotes: 0

Views: 817

Answers (1)

Alan C
Alan C

Reputation: 11

You can only use break stoplabel; inside the stoplabel: code block.

For example:

var cars = ["BMW", "Volvo", "Saab", "Ford"];
var text = [];

list: {
    text.push(cars[0]);
    text.push(cars[1]);
    text.push(cars[2]);
    break list;
    text.push(cars[3]);
    text.push(cars[4]);
    text.push(cars[5]);
}


console.log(text);

You can change your code as below:

var a = 10 ;
function myfn(){
  if(a===15){
    return;
  }
  else{
    console.log(a);
    a++;
    myfn();
  }
}
myfn();

Upvotes: 1

Related Questions