Lessons

JavaScript Basics

Operators in JavaScript

Conditional Statements in JavaScript

JavaScript Strings

JavaScript Arrays

JavaScript Loop

JavaScript Functions

Conclusion

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
1
condition1 && condition2

Example:

javascript
1
2
3
4
5
6
7
8
let age = 25;
let hasID = true;

if (age >= 18 && hasID) {
  console.log("Access granted");
} else {
  console.log("Access denied");
}

Explanation:

  • Both conditions age >= 18 and hasID must be true for the message "Access granted" to be logged. If either condition is false, "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
1
condition1 || condition2

Example:

javascript
1
2
3
4
5
6
7
8
let hasKey = false;
let knowsPassword = true;

if (hasKey || knowsPassword) {
  console.log("You can enter");
} else {
  console.log("You cannot enter");
}

Explanation:

  • If either hasKey or knowsPassword is true, the message "You can enter" will be logged. Only if both are false, "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
1
2
3
4
5
6
7
let isRaining = false;

if (!isRaining) {
  console.log("You don't need an umbrella");
} else {
  console.log("Take an umbrella");
}

Explanation:

  • The condition !isRaining inverts false to true, so the message "You don't need an umbrella" will be logged. If isRaining were true, "Take an umbrella" would be logged.

Combining Logical Operators

You can combine logical operators to evaluate more complex conditions.

Example:

javascript
1
2
3
4
5
6
7
8
9
let age = 30;
let hasLicense = true;
let isSober = false;

if ((age >= 18 && hasLicense) || isSober) {
  console.log("You can drive");
} else {
  console.log("You cannot drive");
}

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
1
2
3
4
5
let username = "";

if (!username) {
  console.log("Username is required");
}

Explanation:

  • Since "" is falsy, !username becomes true, and the message "Username is required" is logged.

Example with Truthy Values:

javascript
1
2
3
4
5
let hasContent = "Hello, world!";

if (hasContent) {
  console.log("Content exists");
}

Explanation:

  • The non-empty string "Hello, world!" is truthy, so "Content exists" will be logged.

Frequently Asked Questions