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 only. If any of its arguments are
true, then 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 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 the time, OR
|| is used in an
if statement to test if any of the 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 does the following:
- Evaluate operands from left to right.
- For each operand, convert it to boolean. If the result is
true, then stop and return the original value of that operand.
- If all other operands have been assessed (i.e. all were
falsy), return 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.
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 a 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
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 does the following:
- Evaluate operands from left to right.
- For each operand, convert it to a boolean. If the result is
false, stop and return the original value of that operand.
- If all other operands have been assessed (i.e. all were truthy), return 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:
&&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 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 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 way to do the same thing – a built-in