Logical Operators
Logical operators are used to combine or invert boolean values. They are commonly used in conditional statements to determine the flow of a program based on multiple conditions. JavaScript provides three logical operators: &&
(logical AND), ||
(logical OR), and !
(logical NOT). Javascript allows you to use the logical operators on Boolean values and non-Boolean values.
Here are the logical operators in JavaScript:
Logical AND (&&)
The logical AND operator (&&
) returns true
if both operands are true
; otherwise, it returns false
. The logical AND operator is used to combine two or more conditions, and it only returns true
if all conditions are true
.
Here is an example of using the logical AND operator:
let x = 5;
let y = 3;
console.log(x > 0 && y > 0); // Output: true
In the example above, the logical AND operator checks if both x
and y
are greater than 0
. Since both conditions are true
, the result is true
.
Logical OR (||)
The logical OR operator (||
) returns true
if at least one of the operands is true
; otherwise, it returns false
. The logical OR operator is used to combine two or more conditions, and it returns true
if any of the conditions are true
.
Here is an example of using the logical OR operator:
let x = 5;
let y = 3;
console.log(x > 0 || y > 0); // Output: true
In the example above, the logical OR operator checks if either x
or y
is greater than 0
. Since both conditions are true
, the result is true
.
Logical NOT (!)
The logical NOT operator (!
) returns true
if the operand is false
, and it returns false
if the operand is true
. The logical NOT operator is used to invert the value of a boolean expression.
Here is an example of using the logical NOT operator:
let x = 5;
let y = 3;
console.log(!(x > 0)); // Output: false
In the example above, the logical NOT operator inverts the result of the condition x > 0
, which is true
. Therefore, the result is false
.
Short-Circuit Evaluation
JavaScript uses short-circuit evaluation to optimize logical expressions. In a logical AND (&&
) expression, if the first operand is false
, the second operand is not evaluated because the result will always be false
. Similarly, in a logical OR (||
) expression, if the first operand is true
, the second operand is not evaluated because the result will always be true
.
Here is an example of short-circuit evaluation with the logical AND operator:
let x = 5;
let y = 3;
console.log(x > 0 && y > 0); // Output: true
Logical Operators with Non-Boolean Values
JavaScript allows you to use the logical operators on non-Boolean values. The logical AND (&&
) and logical OR (||
) operators return the value of one of the operands, not necessarily a boolean value. The logical NOT (!
) operator always returns a boolean value.
In JavaScript we have Falsey values and Truthy values. The following values are considered false
in JavaScript:
- undefined
- null
- 0 (numeric zero)
- NaN (Not-a-Number)
- '' (empty string)
- false
All other values are considered true
. The values which are not Falsey are usually referred to as Truthy values. When you combine a falsey value with a logical operator, the result will be the falsey value. When you combine a truthy value with a logical operator, the result will be the truthy value. Let's look at extensive examples:
console.log(false && true); // Output: false
console.log(false || true); // Output: true
// Falsey values
console.log(false && 'Hello'); // Output: false
console.log(false || 'Hello'); // Output: Hello
// Truthy values
console.log(true && 'Hello'); // Output: Hello
console.log(true || 'Hello'); // Output: true
Explanation:
- The logical AND operator (
&&
) returnsfalse
if one of the operands isfalse
. - The logical OR operator (
||
) returnstrue
if one of the operands istrue
. - when you combine a falsey value with a Truthy value using the logical AND operator, the result will be the falsey value because the logical AND operator returns the first falsey value.
- when you combine a falsey value with a Truthy value using the logical OR operator, the result will be the truthy value because the logical OR operator returns the first truthy value. In the case of
true || 'Hello'
, the result istrue
because the first operand istrue
and remember the logical OR operator returns the first truthy value.
That's it! You now know how to use logical operators in JavaScript to combine or invert boolean values. Happy coding! 🚀
In the next section, we will learn about Bitwise operators in JavaScript. Let's keep going! 🚀
Made with ❤️ by Fasakin Henry