There are various logical operators in javascript,
i) Nullish coalescing operator (??
)
ii)Logical OR operator (||
)
iii)Logical AND operator (&&
)
There are more but we are going to be discussing only these three today.
Nullish coalescing (??
): The nullish coalescing returns the left hand side if the right side is null or undefined. A quick note, it only works on null and undefined not falsy values like 0 or false.
Logical OR operator (||
): The logical OR operator has different use cases. It can be used in normal expressions and it can be used in if statements.
In normal expression, (||
) returns the first truthy value or the last value IF there is no truthy value.
In if statements, (||
) checks if any of the value is true.
Logical AND operator (&&
): The logical AND operator has different use cases. It can be used in normal expressions and it can be used in if statements.
In normal expression, (&&
) returns the first falsy value or the last value IF there is no falsy value.
In if statements, (&&
) checks if all of the values are true.