JAVASCRIPT

  minte9
REMEMBERS




Last update:   17-01-2022

For

Loops are a way to repeat the same code multiple times.
 
/**
 * 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.
 
/**
 * 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);
    }
}

/*
    0
    1
    2
    3
    4
*/

Switch

The value must be of the same type to match.
 
/**
 * In a switch stament the value must be of the ...
 * same type to match.
 * 
 * The ability to group cases is a side-effect of ...
 * how switch works with break.
 * 
 * We can rewrite 'if' with 'switch'
 */

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'

Questions     github Fundamentals
Functions
        A B C D E F
🔔
1/1