Although they are called “logical”, they can be applied to values of any type, not only boolean. The 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, logical OR is meant to manipulate boolean values. If any of it’s arguments is
true, then it returns
true, otherwise – 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 boolean, then it’s converted to boolean for the evaluation.
For instance, a number
1 is treated as
true, a number
0 – as
Most of time, OR
|| is used in
if to test if any of given conditions is correct.
We can pass more conditions:
The logic described above is somewhat classical. Now let’s bring in the “extra” features of JavaScipt.
The extended algorithm works as follows.
Given multiple OR’ed values:
result = value1 || value2 || value3;
"||" operator is doing the following:
- Evalutes operands from left to right.
- For each value – converts it to boolean. If it’s true then stops and returns that value.
- If operands finished, returns the last value.
A value is returned in it’s 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 found.
That leads to some interesting usages compared to a “pure, classical, boolean-only OR”.
Getting the first truthy value from the list of variables or expressions.
Imagine we have several variables, which can either contain the data or be
null/undefined. And we need to choose the first one with data.
We can use OR
defaultUserwere falsy then
"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. The 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.
If we run the example below,
xwould not get assigned:
…And if the first argument is
ORgoes on and evaluates the second one thus running the assignment:
An assignment is a simple case, other side effects can be involved.
As we can see, such use case is a "shorter way to do
if". The first operand is converted to boolean and if it’s false then the second one is evaluated.
Most of time it’s better to use a “regular”
ifto keep the code easy to understand, but sometimes that 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
false – otherwise:
An example with
Just as for OR, any value is allowed as an operand of AND:
Given multiple AND’ed values:
result = value1 && value2 && value3;
"&&" operator is doing the following:
- Evalutes operands from left to right.
- For each value converts it to a boolean. If the result is
false, stops and returns the original value.
- If values finished (all are truthy), returns the last one.
In other words, AND returns the first falsy value or the last value if none 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:
&&executes before OR
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 to be shorter. But
if is more obvious and tends to be a little bit more readable.
So it is recommended to use every construct for it’s 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 an 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, at the end we have a plain value-to-boolean conversion.
There’s a little more verbose to do the same – a built-in