== // equal to
=== // equal value and equal type
!= // not equal
!== // not equal value or not equal type
> // greater than
< // less than
>= // greater than or equal to
<= // less than or equal to
? // ternary operator | if true ? execute : false |
Used to compare Javascript values, returns boolean.
&& // and, if both are true
|| // or, if either are true
! // not, truthy returns false, falsy returns true
Used to determine logic between variables or values.
false, 0, ' ', null, undefined, NaN
Above are falsy values, everything else is truthy.
The if Statement
if (condition) {
// executed if condition is true
}
The else Statement
if (condition) {
// executed if condition is true
} else {
// executed if condition is false
}
The else if Statement
if (condition1) {
// executed if condition1 is true
} else if (condition2) {
// executed if condition1 is false and condition2 is true
} else {
// executed if condition1 is false and condition2 is false
}
Nested if else Statement
if (condition1) {
// executed if condition1 is true
} else {
if (nestedCondition) {
// executed if nestedCondition is true
} else {
// executed if nestedCondition is false
}
}
The if...else statements executes different statements based on different conditions based on whether they are truthy or falsy.
switch(value to be matched) {
case 'a':
// executes if value is a
break;
case 'b':
case 'c':
// executes if value is b or c
break;
default:
// executes if no match
}
Switch statements take in a value, a changing variable maybe, to match against cases to execute code.
condition ? true code : false code ;
condition ? true code : 2nd condition ? true code : false code ; // to chain ternaries
Used as an alternative to if else statements.
const result = false || true; // returns the first true one with the or operator ||
const result = isEmailVerified && ‘johnDoe@gmail.com’; // Returns ‘johnDoe@gmail.com’ if isEmailVerified is true;
const username = isEmailVerified && response || "guest"; // if false returns 'guest' otherwise returns response
Short circuiting are shorter conditional statements.