JavaScript Conditional #
if #
if (<condition>) {
...;
}
Example:
let some_var = true;
if (some_var) {
console.log('sup');
}
if-else #
if {<conditional>} {
...;
} else {
...;
};
Ternary operator #
Super condensation of conditionals.
Goes from:
if (<condition>) {
<thing1>;
} else {
<thing2>;
}
to this:
<condition> ? <thing1> : <thing2>;
else-if #
More conditionals. The first that evaluates true executes.
if (<condition1>) {
...;
} else if (<condition2>) {
...;
} else {
...;
}
switch #
A cleaner else if.
switch (<thing to be compared against for equivalence>) { // argument can just be a value
case <what the argument gets compared against>: // just be a value
...; // do something
break;
case <another point of comparison>:
...;
break;
default: // for when nothing is true or matches up
...;
break;
}
break exits code and doesn’t run more.
Comparison #
-
< -
> -
<= -
=> -
===- equivalent -
!==- not equivalent
Boolean #
-
&&- and -
||- or -
!- not
Truthy vs. Falsy #
A variable is truthy if it’s been assigned something not falsy.
What’s falsy? Stuff like:
-
0
-
empty strings
-
null -
undefined
-
NaN
Good for shorthand syntax.
Sidenote: short-circuit evaluation.