JavaScript Logical Operators – What Is a JavaScript Logic?
Logic is the analytical action performed by a computer, which often requires the computer to decide if a specific condition is true or false.
Here’s an example:
In the snippet above, we used the strict equality operator (===
) to analyze senseOrgans
content—to check if it is truly equal to 100
.
The result of the logical comparison—between number 100
and senseOrgans
content—returned false
because 100
and 5
are not equal.
So, now that we know what logic is, let’s discuss the common types of logical operators in JavaScript, starting with the equality operator.
Equality Operator (==
)
The equality operator (==
) checks if its two operands are of equal value.
Here’s an example:
The equality comparison between century
’s content and "100"
returns true
because the two values are equal.
Strict Equality Operator (===
)
The strict equality operator (===
) checks if its two operands are strictly of equal type and value.
Here’s an example:
The equality comparison between century
’s content and "100"
returned false
because the two operands have equal values but different types.
In other words, the content in century
is a number type, while "100"
is a string type.
NOT Operator (!
)
The NOT operator (!
) is used to negate the Boolean value of an expression. In other words, it converts true to NOT true and false to NOT false.
Here’s an example:
The code above means NOT true
. Therefore, if you run the snippet, the computer will return false
.
Here’s another example:
In the snippet above, century !== 100
means century
NOT strictly equal to 100
.
The computer returned false
because we used the NOT operator to negate the strict equality operator.
AND Operator (&&
)
The AND operator (&&
) checks if all of its operands are true.
Here’s an example:
If you run the snippet above, the computer will return true
because the left and right operands of the AND operator are true.
Technically, the AND (&&
) operator returns its right operand’s value if its left operand’s value is truthy. Otherwise, if the left operand is falsy, the AND operator will return the left operand’s value.
For instance, consider the code below:
In the snippet above, observe that the computer returns the right operand whenever the left operand is truthy. However, whenever the left is falsy, the computer returns the value of that left operand.
AND Assignment Operator (&&=
)
The AND assignment operator (&&=
) checks if its left-hand side expression is truthy. If so, it initializes the expression with its right-hand side operand.
However, the operator will not update the left-hand side expression if its current value is falsy.
Example 1: AND assignment operator used to initialize a falsy left-hand side expression
Example 2: AND assignment operator used to initialize a truthy left-hand side expression
Example 3: AND assignment operator used to initialize a truthy left-hand side expression
OR Operator (||
)
The OR operator (||
) checks and returns true
if one (or both) of its operands is true. Otherwise, it returns false
if both of its operands are false.
Here’s an example:
If you run the snippet above, the computer will return true
because one of the OR operator’s operands is true.
Technically, the OR (||
) operator returns the left operand if its value is truthy.
Otherwise, if the left operand is falsy, the OR operator will return the right operand’s value.
For instance, consider the code below:
In the snippet above, observe that the computer returns the left operand if it is truthy. Otherwise, it returns the right operand.
Design and develop at the same time
OR Assignment Operator (||=
)
The OR assignment operator (||=
) checks if its left-hand side expression is falsy. If so, it initializes the expression with its right-hand side operand.
However, the operator will not update the left-hand side expression if its current value is truthy.
Example 1: OR assignment operator used to initialize a falsy left-hand side expression
Example 2: OR assignment operator used to initialize a truthy left-hand side expression
Example 3: OR assignment operator used to initialize a truthy left-hand side expression
Nullish Coalescing Operator (??
)
The nullish coalescing operator (??
) checks if its left-hand side operand is undefined
or null
. If so, it returns its right-hand side operand. Otherwise, it produces its left-hand side operand.
Here’s an example:
The snippet above will return true
because the left-hand side operand is neither undefined
nor null
. So, the left operand gets returned.
The nullish coalescing operator is similar to the OR operator. The main difference is that nullish returns its right-hand side operand’s value if the left operand is nullish (undefined
or null
).
However, the OR operator returns its right-hand side operand’s value if the left operand is any falsy value—not just undefined
or null
.
Here’s another example:
In the snippet above, observe that the computer returns the right operand only if the left one is null
or undefined
. Otherwise, it returns the left operand.
Nullish Assignment Operator (??=
)
The nullish assignment operator (??=
) checks if its left-hand side expression is undefined
or null
. If so, it initializes the expression with its right-hand side operand.
However, the operator will not update the left-hand side expression if its current value is not nullish (undefined
or null
).
Example 1: Nullish assignment operator used to initialize a non-nullish left-hand side expression
Learn CSS Grid with Images
Example 2: Nullish assignment operator used to initialize a nullish left-hand side expression
Example 3: Nullish assignment operator used to initialize a nullish left-hand side expression
Greater than Operator (>
)
The greater than operator (>
) checks if its left operand is greater than its right-hand one. If so, the Boolean value true
gets returned. Otherwise, the computer will return false
.
Here’s an example:
If you run the snippet above, the computer will return false
because the left-hand side operand is not greater than the one on the right-hand.
Less than Operator (<
)
The less than operator (<
) checks if its left operand is less than its right-hand one. If so, the Boolean value true
gets returned. Otherwise, the computer will return false
.
Here’s an example:
If you run the snippet above, the computer will return true
because the left-hand side operand is less than the one on the right-hand.
Greater than or Equal to Operator (>=
)
The greater than or equal to operator (>=
) checks if its left operand is greater than or equal to its right-hand operand. If so, the Boolean value true
gets returned. Otherwise, the computer will return false
.
Here’s an example:
If you run the snippet above, the computer will return true
because the left-hand side operand is equal to the one on the right-hand.
Less than or Equal to Operator (<=
)
The less than or equal to operator (<=
) checks if its left operand is less than or equal to the one on the right-hand. If so, the Boolean value true
gets returned. Otherwise, the computer will return false
.
Here’s an example:
If you run the snippet above, the computer will return true
because the left-hand side operand is equal to the one on the right-hand.
NOT Equal to Operator (!=
)
The NOT equal to operator (!=
) checks if its two operands are not of equal value. If so, the Boolean value true
gets returned. Otherwise, the computer will return false
.
Here’s an example:
The snippet above will return true
because the left and right operands are not of equal value.
Here’s another example:
The snippet above will return false
because the left and right operands are of equal value.
NOT Strictly Equal to Operator (!==
)
The Not strictly equal to operator (!==
) checks if its two operands are strictly not of equal type and equal value. If so, the Boolean value true
gets returned. Otherwise, the computer will return false
.
Here’s an example:
If you run the snippet above, the computer will return true
because the left and right operands are of equal values but not of similar types. Therefore, they are not strictly equivalent to one another.