Although they are called “logical”, they can be applied to values of any type, not only boolean. Their result can also be of any type.
Let’s see the details.
The “OR” operator is represented with two vertical line symbols:
result = a || b;
In classical programming, the logical OR is meant to manipulate boolean values only. If any of its arguments are
true, it returns
true, otherwise it returns
There are four possible logical combinations:
As we can see, the result is always
true except for the case when both operands are
If an operand is not a boolean, it’s converted to a boolean for the evaluation.
For instance, the number
1 is treated as
true, the number
Most of the time, OR
|| is used in an
if statement to test if any of the given conditions is
We can pass more conditions:
The extended algorithm works as follows.
Given multiple OR’ed values:
result = value1 || value2 || value3;
|| operator does the following:
- Evaluates operands from left to right.
- For each operand, converts it to boolean. If the result is
true, stops and returns the original value of that operand.
- If all operands have been evaluated (i.e. all were
false), returns the last operand.
A value is returned in its original form, without the conversion.
In other words, a chain of OR
"||" returns the first truthy value or the last one if no such value is found.
This leads to some interesting usage compared to a “pure, classical, boolean-only OR”.
Getting the first truthy value from a list of variables or expressions.
Imagine we have several variables which can either contain data or be
null/undefined. How can we find the first one with data?
We can use OR
"unnamed"would be the result.
Operands can be not only values, but arbitrary expressions. OR evaluates and tests them from left to right. The evaluation stops when a truthy value is reached, and the value is returned. This process is called “a short-circuit evaluation” because it goes as short as possible from left to right.
This is clearly seen when the expression given as the second argument has a side effect like a variable assignment.
In the example below,
xdoes not get assigned:
If, instead, the first argument is
||evaluates the second one, thus running the assignment:
An assignment is a simple case. Other side effects can also be involved.
As we can see, such a use case is a "shorter way of doing
if". The first operand is converted to boolean. If it’s false, the second one is evaluated.
Most of time, it’s better to use a “regular”
ifto keep the code easy to understand, but sometimes this can be handy.
The AND operator is represented with two ampersands
result = a && b;
In classical programming, AND returns
true if both operands are truthy and
An example with
Just as with OR, any value is allowed as an operand of AND:
Given multiple AND’ed values:
result = value1 && value2 && value3;
&& operator does the following:
- Evaluates operands from left to right.
- For each operand, converts it to a boolean. If the result is
false, stops and returns the original value of that operand.
- If all operands have been evaluated (i.e. all were truthy), returns the last operand.
In other words, AND returns the first falsy value or the last value if none were found.
The rules above are similar to OR. The difference is that AND returns the first falsy value while OR returns the first truthy one.
We can also pass several values in a row. See how the first falsy one is returned:
When all values are truthy, the last value is returned:
&&is higher than OR
The precedence of AND
&& operator is higher than OR
So the code
a && b || c && d is essentially the same as if the
&& expressions were in parentheses:
(a && b) || (c && d).
Just like OR, the AND
&& operator can sometimes replace
The action in the right part of
&& would execute only if the evaluation reaches it. That is, only if
(x > 0) is true.
So we basically have an analogue for:
The variant with
&& appears shorter. But
if is more obvious and tends to be a little bit more readable.
So we recommend using every construct for its purpose: use
if if we want if and use
&& if we want AND.
The boolean NOT operator is represented with an exclamation sign
The syntax is pretty simple:
result = !value;
The operator accepts a single argument and does the following:
- Converts the operand to boolean type:
- Returns the inverse value.
A double NOT
!! is sometimes used for converting a value to boolean type:
That is, the first NOT converts the value to boolean and returns the inverse, and the second NOT inverses it again. In the end, we have a plain value-to-boolean conversion.
There’s a little more verbose way to do the same thing – a built-in
The precedence of NOT
! is the highest of all logical operators, so it always executes first, before