minte9
LearnRemember



For

Loops are a way to repeat the same code multiple times.
 
/**
 * For looop
 * 
 * A single execution of the loop body is called an iteration
 * The counter variable i is declared in the loop
 */

for (let i=0; i<3; i++) {
    console.log(i); // 0 1 2
}

Labels

Sometimes we need to break out from multiple nested loops.
 
/**
 * For label
 * 
 * A label is an identifier with a colon before the loop
 * Labels are not to jump into an arbitrary place in the code
 */

outer: while(true) {
    inner: for(let i=0; i<10; i++) {
        if (i == 5) {
            break outer;
        }
        console.log(i); // 1 1 2 3 4
    }
}

Switch

The value must be of the same type to match.
 
/**
 * Switch statement
 * 
 * The value must be of the same type to match
 * The ability to group cases is a side-effect of how switch works
 */

let a = 2;

switch(a) {
    case   2: // group of two cases (no break)
    case '2':
        console.log("2 or '2'");
        break;
    default:
        console.log('no value'); // 2 or '2'
}



  Last update: 303 days ago