Javascript conditionals: Difference between revisions

From wikinotes
No edit summary
 
No edit summary
Line 1: Line 1:
= If Not =
<blockquote>
<syntaxhighlight lang="javascript">
if !(foo == "bar") { ... }
</syntaxhighlight>
</blockquote><!-- If Not -->
= If Statements =
= If Statements =
<blockquote>
<blockquote>

Revision as of 22:57, 14 September 2021

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'