Javascript conditionals

From wikinotes
Revision as of 22:57, 14 September 2021 by Will (talk | contribs)

If Not

if !(foo == "bar") { ... }

If Statements

if (var == "one") {
    // ...
} else if (var == "two") {
    // ...
} else {
    // ...
}

case/switch statements

switch (var) {
    case 'one':
        // ...
        break;
    case 'two':
        // ...
        break;
    default:
        // ...
}

Ternary Condition

If id is 1, name is 'will', otherwise is 'guest'

let name = (id == 1) ? 'will' : 'guest'