Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is : any operator?

If I put this line into JavaScript console (you do not need to declare "foo")

foo : 4;

What exactly this line means? Does "foo" live in any context? Is : any operator?

like image 630
Cibo FATA8 Avatar asked Mar 13 '26 10:03

Cibo FATA8


1 Answers

This is a label:

From the docs:

The labeled statement can be used with break or continue statements. It is prefixing a statement with an identifier which you can refer to.

In other programming languages like C labels are used often with the goto statement. JavaScript do not have goto. In javaScript it can be used with break or continue statements.

The example from the docs using a labeled continue with for loops:

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;
      }
      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"
like image 52
Bruno Peres Avatar answered Mar 15 '26 00:03

Bruno Peres