JavaScript - Q&A
JavaScript - Q&A

Does JavaScript support goto?



Unlike C/C++, JavaScript does not have goto statement. This could be due to the reason that it makes difficult to trace the control flow of a program, making hard to understand and modify the program. Although, JavaScript supports label statement which can be used to get the desired result.

The label statement is used with break or continue statements. It is used to prefix a statement with an identifier which can be referred. A label can be specified by any name other than the reserved words in JavaScript. The syntax for using label is given below:

Syntax

label :
  statements;

Label statement with Continue statement

A label is used to identify a loop, and then continue statement is used to indicate when to skip the current iteration.

var txt = "";

loop1 :
for (i = 1; i <= 5; i++){
  if(i == 3)
    continue loop1;
  txt = txt + "i = " + i + "<br>";
}

The output (value of txt) after running above script will be:

i = 1
i = 2
i = 4
i = 5

In the example below, label statement is used to skip the inner and outer loop respectively whenever the conditions are met.

var txt ="#Skips the inner loop<br>";

loop1 :
for (i = 1; i <= 3; i++){
  loop2 :
  for (j = 1; j <= 3; j++){
    if(i == 2 && j == 2)
      continue loop2; 
    txt = txt + "i = " + i + ", j = " + j +"<br>"; 
  }
}

txt = txt + "<br>#Skips the outer loop<br>";
loop3 :
for (i = 1; i <= 3; i++){
  loop4 :
  for (j = 1; j <= 3; j++){
    if(i == 2 && j == 2)
      continue loop3; 
    txt = txt + "i = " + i + ", j = " + j +"<br>"; 
  }
}

The output (value of txt) after running above script will be:

#Skips the inner loop
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1
i = 2, j = 3
i = 3, j = 1
i = 3, j = 2
i = 3, j = 3

#Skips the outer loop
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1
i = 3, j = 1
i = 3, j = 2
i = 3, j = 3

Label statement with Break statement

A label is used to identify a loop, and then break statement is used to indicate when to get out of the loop.

var txt = "";

loop1 :
for (i = 1; i <= 5; i++){
  if(i == 3)
    break loop1;
  txt = txt + "i = " + i + "<br>";
}

The output (value of txt) after running above script will be:

i = 1
i = 2

In the example below, label statement is used to skip the inner and outer loop respectively whenever the conditions are met.

var txt ="#Breaks from the inner loop<br>";

loop1 :
for (i = 1; i <= 3; i++){
  loop2 :
  for (j = 1; j <= 3; j++){
    if(i == 2 && j == 2)
      break loop2; 
    txt = txt + "i = " + i + ", j = " + j +"<br>"; 
  }
}

txt = txt + "<br>#Breaks from the outer loop<br>";
loop3 :
for (i = 1; i <= 3; i++){
  loop4 :
  for (j = 1; j <= 3; j++){
    if(i == 2 && j == 2)
      break loop3; 
    txt = txt + "i = " + i + ", j = " + j +"<br>"; 
  }
}

The output (value of txt) after running above script will be:

#Breaks from the inner loop
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1
i = 3, j = 1
i = 3, j = 2
i = 3, j = 3

#Breaks from the outer loop
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1

5