Skip to content

Instantly share code, notes, and snippets.

@simplifyjs
Created December 6, 2019 02:45
Show Gist options
  • Save simplifyjs/ae11b7298158a82457e100fa5c5b5f1d to your computer and use it in GitHub Desktop.
Save simplifyjs/ae11b7298158a82457e100fa5c5b5f1d to your computer and use it in GitHub Desktop.
// Break in outer for loop
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);
/*
Result:
"I: 0"
"JJ: 0"
"JJ: 1"
"STR: 0"
*/
// Continue in outer for loop
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);
/*
Result:
"I: 0"
"JJ: 0"
"JJ: 1"
"I: 1"
"JJ: 0"
"JJ: 1"
"I: 2"
"JJ: 0"
"JJ: 1"
"STR: 0"
*/
// 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);
/*
Result:
"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);
/*
Result: Infinite loop
- It never reach the increment value of "a" variable
*/
// If statement
foo: if(true) {
console.log('first');
break foo;
console.log('middle');
}
console.log('last');
/*
Result:
"first"
"last"
*/
// Object
mylabel: {
console.log('first');
break mylabel;
console.log('middle');
}
console.log('last');
/*
Result:
"first"
"last"
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment