LABEL WITH BREAK / CONTINUE IN FOR LOOP
Break outerloop
var str = 0;
outerloop:
for (var i = 0; i < 3; i++) {
console.log("I: " + i);
innerloop:
for(var j = 0; j < 5; j++) {
if(j === 2) {
break outerloop;
}
console.log("JJ: " + j);
}
str++;
}
console.log('STR: ' + str);
/*
"I: 0"
"JJ: 0"
"JJ: 1"
"STR: 0"
*/
Continue outerloop
var str = 0;
outerloop:
for (var i = 0; i < 3; i++) {
console.log("I: " + i);
innerloop:
for(var j = 0; j < 5; j++) {
if(j === 2) {
continue outerloop;
}
console.log("JJ: " + j);
}
str++;
}
console.log('STR: ' + str);
/*
"I: 0"
"JJ: 0"
"JJ: 1"
"I: 1"
"JJ: 0"
"JJ: 1"
"I: 2"
"JJ: 0"
"JJ: 1"
"STR: 0"
*/
Break innerloop
var str = 0;
outerloop:
for (var i = 0; i < 3; i++) {
console.log("I: " + i);
innerloop:
for(var j = 0; j < 5; j++) {
if(j === 2) {
break innerloop;
}
console.log("JJ: " + j);
}
str++;
}
console.log('STR: ' + str);
/*
"I: 0"
"JJ: 0"
"JJ: 1"
"I: 1"
"JJ: 0"
"JJ: 1"
"I: 2"
"JJ: 0"
"JJ: 1"
"STR: 3"
*/
Continue innerloop
var str = 0;
outerloop:
for (var i = 0; i < 3; i++) {
console.log("I: " + i);
innerloop:
for(var j = 0; j < 5; j++) {
if(j === 2) {
continue innerloop;
}
console.log("JJ: " + j);
}
str++;
}
console.log('STR: ' + str);
/*
"I: 0"
"JJ: 0"
"JJ: 1"
"JJ: 3"
"JJ: 4"
"I: 1"
"JJ: 0"
"JJ: 1"
"JJ: 3"
"JJ: 4"
"I: 2"
"JJ: 0"
"JJ: 1"
"JJ: 3"
"JJ: 4"
"STR: 3"
*/
LABEL WITH BREAK / CONTINUE IN WHILE LOOP
Break outerloop
var a = 0;
outerloop:
while(a < 5) {
console.log('A-first: ' + a);
for(var b = 0; b < 3; b++) {
console.log('--B-first: ' + b);
if(b === 2) {
break outerloop;
console.log('--B-last: '+ b);
}
}
console.log('A-last: ' + a);
a++;
}
console.log('Final A: ' + a);
/*
"A-first: 0"
"--B-first: 0"
"--B-first: 1"
"--B-first: 2"
"Final A: 0"
*/
Continue outerloop
var a = 0;
outerloop:
while(a < 5) {
console.log('A-first: ' + a);
for(var b = 0; b < 3; b++) {
console.log('--B-first: ' + b);
if(b === 2) {
continue outerloop;
console.log('--B-last: '+ b);
}
}
console.log('A-last: ' + a);
a++;
}
console.log('Final A: ' + a);
// Infinite loop
LABELED BLOCK WITH BREAK
// If statement
foo: if(true) {
console.log('first');
break foo;
console.log('middle');
}
console.log('last');
/*
"first"
"last"
*/
// Object
mylabel: {
console.log('first');
break mylabel;
console.log('middle');
}
console.log('last');
/*
"first"
"last"
*/
LABEL IN NON-STRICT MODE FOR FUNCTIONS
Function declaration
label1: function myfunction() {}
Function generators
label1: function* myfunction() {}
Error when it is set in strict mode
'use strict';
label1: function myfunction(){}
// SyntaxError
Here is the gist compilation of codes