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?
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With