Javascript conditionals: Difference between revisions

From wikinotes
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
= If Not =
= If Not =
<blockquote>
<blockquote>
Not requires an additional set of brackets.
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
if !(foo == "bar") { ... }
if (!(foo == "bar")) { ... }
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- If Not -->
</blockquote><!-- If Not -->
Line 29: Line 31:
         // ...
         // ...
         break;
         break;
    case 'three': case: 'four:
        // ...
     default:
     default:
         // ...
         // ...

Latest revision as of 20:07, 18 December 2022

If Not

Not requires an additional set of brackets.

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

If Statements

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

case/switch statements

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

Ternary Condition

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

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