They are called “logical”, but can be applied to values of any type, their result can also be of any type.
The OR operator is performed with two vertical lines and can manipulate boolean values only. The logical || (OR) can manipulate boolean values only. In case any of its arguments are true, it returns true, if not, it returns false. With this operator, at least one of the statements has to evaluate to true, otherwise it returns false.
boolValue = a || b;
There are four possible logical combinations:
As you see, the result is always true except for the case when both operands are false. If an operand isn’t a boolean, then it’s converted to a boolean for the rating.
For instance, the number 1 is attended as true, the number 0 as false:
Sometimes || (OR) is used in an if statement to test if any of the given conditions is true.
|| (OR) finds the first truthy value
Given multiple OR’ed values:
result = value1 || value2 || value3;
The || (OR) operator executes the following things:
- Evaluates operands from left to right.
- For each operand, converts it to boolean. In case the result is true, it stops and returns the original value of that operand.
- If all operands are false, it returns the last operand.
A value is returned in its initial form, without any changes.
The AND operator is performed with two ampersands &&and returns true if both operands are truthy and false otherwise:
Example of the AND operator with if:
&& (AND) finds the first falsy value
Given multiple AND’ed values:
result = value1 && value2 && value3;
The AND && operator makes the following:
- Asses operands from left to right.
- Converts it to a boolean for each operand. In case the result is false, it stops and returns the original value of that operand.
- If all operands were truthy, it returns the last operand.
Shortly, AND returns the first falsy value or the last value if none were found.
The rules above are similar to OR. The difference between them is that AND returns the first falsy value while OR returns the first truthy one.
AND returns the second operand if the first operand is truthy:
AND returns the first operand, if it is false, the second operand is ignored:
The logical NOT operator is represented with an exclamation sign ! (NOT) . It reverses or negates the value of a boolean.
It’s syntax is very simple:
result = !value;
The operator accepts a single argument and does presented below functions:
- Transforms the operand to boolean type: true/false.
- Returns the
Example of the NOT operator:
console.log( !false ); // true
console.log( !1 ); // false
Sometimes we use a double !! (NOT) to convert a value to boolean type:
The first NOT transforms the value to boolean and returns the opposite, the second NOT inverses it again. After all, we have a plain value-to-boolean conversion.
There’s a little more wordy way to do the same thing – a built-in Boolean function:
The priority of ! (NOT) is the highest of all logical operators, that’s why it always executes first, before && or ||.