JavaScript Logical Operators
Logical Operators in JavaScript
JavaScript logical operators are used to perform logical operations on values, typically within conditional statements. These operators return a Boolean value (true
or false
) based on the logic they evaluate. Understanding these operators is crucial for controlling the flow of your JavaScript programs.
1. && (Logical AND)
The &&
operator returns true
if both operands are true
. If either operand is false
, the result is false
.
Syntax:
javascript
1condition1 && condition2
Example:
javascript
1let age = 25;
2let hasID = true;
3
4if (age >= 18 && hasID) {
5 console.log("Access granted");
6} else {
7 console.log("Access denied");
8}
Explanation:
- Both conditions
age >= 18
andhasID
must betrue
for the message"Access granted"
to be logged. If either condition isfalse
,"Access denied"
will be logged.
2. || (Logical OR)
The ||
operator returns true
if at least one of the operands is true
. If both operands are false
, the result is false
.
Syntax:
javascript
1condition1 || condition2
Example:
javascript
1let hasKey = false;
2let knowsPassword = true;
3
4if (hasKey || knowsPassword) {
5 console.log("You can enter");
6} else {
7 console.log("You cannot enter");
8}
Explanation:
- If either
hasKey
orknowsPassword
istrue
, the message"You can enter"
will be logged. Only if both arefalse
,"You cannot enter"
will be logged.
3. ! (Logical NOT)
The !
operator negates a Boolean value. It returns true
if the operand is false
, and false
if the operand is true
.
Syntax:
javascript
1!condition
Example:
javascript
1let isRaining = false;
2
3if (!isRaining) {
4 console.log("You don't need an umbrella");
5} else {
6 console.log("Take an umbrella");
7}
Explanation:
- The condition
!isRaining
invertsfalse
totrue
, so the message"You don't need an umbrella"
will be logged. IfisRaining
weretrue
,"Take an umbrella"
would be logged.
Combining Logical Operators
You can combine logical operators to evaluate more complex conditions.
Example:
javascript
1let age = 30;
2let hasLicense = true;
3let isSober = false;
4
5if ((age >= 18 && hasLicense) || isSober) {
6 console.log("You can drive");
7} else {
8 console.log("You cannot drive");
9}
Explanation:
- The condition checks if the person is an adult with a license or is sober. If any of these combined conditions evaluate to
true
,"You can drive"
will be logged.
Truthy and Falsy Values
Logical operators work with more than just Boolean values. JavaScript treats certain values as "truthy" or "falsy" in logical operations.
Falsy values:
false
0
""
(empty string)null
undefined
NaN
Example with Falsy Values:
javascript
1let username = "";
2
3if (!username) {
4 console.log("Username is required");
5}
Explanation:
- Since
""
is falsy,!username
becomestrue
, and the message"Username is required"
is logged.
Example with Truthy Values:
javascript
1let hasContent = "Hello, world!";
2
3if (hasContent) {
4 console.log("Content exists");
5}
Explanation:
- The non-empty string
"Hello, world!"
is truthy, so"Content exists"
will be logged.