Using the logical operator, you can easily perform operations based on whether two operands are
|&&||AND||x && y|
|||||OR||x || y|
Of course, the table only shows you so much. If you feel like you need to learn more about logical operators, be sure to continue reading.
We will cover each of these operators showing you their possible results and how to use them.
We have also included a truth table for each value so you can see the possible results you will get depending on the values fed to the operator.
AND Operator (
The “AND” logical operator (
If both values are
true, then the operator will return
true. If either value is
false, then the operator will return
The logical AND operator is written first with the left operand, then the two ampersand symbols (
&&), and finally the right operand. The left operand will be compared to the right operand.
a && b
Using this you can see that it will only ever return
true if both operands are
This can be helpful to know when using “AND” to compare the results from two functions. Using the more lightweight function as the first call can help save some CPU time.
Example of the AND Operator
console.log()“, we can easily see the values returned by this logical operator.
- The first usage of the “AND” logical operator will return
This returns true because the left and right operands are “
- The second expression will return
This is because one of the operands is “
false“. As mentioned earlier, the “AND” operator requires both operands to be
console.log(true && true); console.log (false && true);
&&) logical operator in both of our examples.
Logical OR Operator (
Using the OR operator (
true if either the left or right operand is
||), and finally the right operand.
a || b
To understand the logical output of the OR operator, you can view the table below. The table below shows what the or operator (
||) will return depending its operands’ value.
In this table, the left operand will be called “
a“, and the right operand will be called “
This table shows that the OR operator returns
true as long as one operand is
true. After that, it will only check the right operator if the left operand is
If you have a function that returns fast, you might want to utilize it as the left operand, allowing the OR operator to return quickly if the value is
Example of the OR (
In the example below, we have three different usages of the OR operator. We will quickly explain the result of each of these expressions and why.
The or operator is satisfied by the left operand being true. While the right operand is also
- The next example will also return
While the left operand is
false, the right operand is
true, satisfying the requirements of the logical OR operator.
- The final example returns
falsesince both the left and right operands are false.
console.log(true || true); console.log(false || true); console.log(false || false);
Thanks to us using the “
true true false
Logical NOT Operator (
!) allows you to check if a value is not
true. You can almost consider this operator to return the inverse of whatever logic was returned.
One thing to note is that this logical operator only works on a single operand at a time. If you want to get the inverse of multiple operands, you must use this operator on each of them.
!), followed by your operand.
The table below shows how the NOT operator returns
true when the operand is
false. It will also return
false when the operand is
Example of the Logical NOT Operator (
To showcase this, let us write a short example showing what is returned by the NOT operator.
- The first example will return false since we use the logical NOT operator on a “true” value.
- With the second example, the NOT operator will return
true. This is because we are using it on a “
Below you can see the results of our NOT expressions logged to the console.
You should now know how to use the logical NOT, OR, and AND operators within your code.
Please comment below if you have any questions about logical operators.