Logical operators in JavaScript allow us to work with boolean values (true or false) and perform comparison and combination operations on conditions.
The most common logical operators are && (AND), || (OR), and ! (NOT), which allow us to construct more complex logical expressions.
The following table presents the logical operators in JavaScript:
| Operator | Name | Description |
|---|---|---|
&& | AND | Returns true if both operands are true |
|| | OR | Returns true if at least one of the operands is true |
! | NOT | Negates the value |
If you want to learn more, check out the Introduction to Programming Course
List of Logical Operators
AND Operator (&&)
The AND operator && returns true if both expressions it combines are true, and false otherwise.
const x = 5;
const y = 10;
if (x > 0 && y > 0) {
console.log('Both variables are positive');
} else {
console.log('At least one variable is not positive');
}
In this example, the expression x > 0 && y > 0 evaluates whether both x and y are greater than zero. If both conditions are true, the message “Both variables are positive” will be printed to the console.
OR Operator (||)
The OR operator || returns true if at least one of the expressions it combines is true, and false if both are false.
const age = 25;
if (age < 18 || age > 65) {
console.log('You have an age that may benefit you from discounts');
} else {
console.log('You do not qualify for age discounts');
}
In this example, the expression edad < 18 || edad > 65 evaluates whether the age is less than 18 or greater than 65.
If at least one of these conditions is true, the message “You are of an age that may qualify for discounts” will be printed.
NOT Operator (!)
The NOT operator ! is used to invert the boolean value of an expression. If the expression is true, the NOT operator makes it false, and vice versa.
const raining = false;
if (!raining) {
console.log('You are not taking an umbrella');
} else {
console.log('Take your umbrella with you');
}
In this example, !llueve evaluates whether llueve is false. If so, the message “You are not taking an umbrella” will be printed, indicating that there is no need to carry an umbrella if it’s not raining.
Combining Logical Operators
We can combine multiple logical operators in a single expression to build more complex conditions.
const temperature = 25;
const rain = true;
if (temperature > 20 && !rain) {
console.log('It is a good day to go for a walk');
} else {
console.log('Better to stay at home');
}
In this example, the expression temperatura > 20 && !lluvia evaluates whether the temperature is greater than 20 degrees and whether it is not raining. If both conditions are true, the message “It’s a good day to go for a walk” will be printed.
In JavaScript, conditions are evaluated using short-circuit evaluation. This means that:
- In an expression with
&&, if the first operand isfalse, the second operand is not evaluated. - In an expression with
||, if the first operand istrue, the second operand is not evaluated.
