A colon is used to add a label. As explained by the MDN documentation for the label :
Provides an operator with an identifier that you can reference using the break or continue statement.
For example, you can use the label to identify the loop, and then use the break or continue statements to indicate whether the program should interrupt the loop or continue execution.
MDN also contains sample code:
var i, j; loop1: for (i = 0; i < 3; i++) { //The first for statement is labeled "loop1" loop2: for (j = 0; j < 3; j++) { //The second for statement is labeled "loop2" if (i == 1 && j == 1) { continue loop1; } else { console.log("i = " + i + ", j = " + j); } } } // Output is: // "i = 0, j = 0" // "i = 0, j = 1" // "i = 0, j = 2" // "i = 1, j = 0" // "i = 2, j = 0" // "i = 2, j = 1" // "i = 2, j = 2" // Notice how it skips both "i = 1, j = 1" and "i = 1, j = 2"
In addition, as indicated in the article, such logic tends to make your code more understandable, so you better not restructure the code to use other types of flow control (functions, etc.) to avoid the need to use labels.
Note that this syntax can be confusing, as it is also similar to object syntax, for example:
{ js: {1}, ... }
source share