Logical Operators ! (NOT), && (AND), || (OR)
If you are learning to code or new to coding, you will be using logical operators, these ar...
For further actions, you may consider blocking this person and/or reporting abuse
Great article! Don't forget that && and || use short-circuit evaluation, useful for use cases like feature flags and set default values:
Yes!
As a reminder, the logical operators work the following way with non-boolean values:
expr1 && expr2
: Ifexpr1
is truthy, returnsexpr2
; else, returnsexpr1
.expr1 || expr2
: Ifexpr1
is truthy, returnsexpr1
; else, returnsexpr2
.Hi André! Thank you for your comment. With just some lines you have provided a great input about default values.
Great post! Just a heads up that you can highlight code like so if you'd like:
Hi Peter! Thanks for the advise, I for sure will start doing it.
The bitwise operator
^
(XOR) can also be used to evaluate if only one of both given expressions is true and can be a useful shorthand:Since it is not a logical operator, it will cast to Number (0 = false, 1 = true), but
if
will accept any true-ish value, so you only need to cast to boolean if you actually require the result to be one.Unfortunately, there's no logical XOR operator in JS as of now.
Thanks for your input Alex! You have made more robust the post with your comment, I appreciate you took the time.