Loading...

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 lines
|
6/ 500 tokens
1
condition1 && condition2
Code Tools

Example:

javascript
8 lines
|
34/ 500 tokens
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");
}
Code Tools

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 lines
|
6/ 500 tokens
1
condition1 || condition2
Code Tools

Example:

javascript
8 lines
|
39/ 500 tokens
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");
}
Code Tools

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 lines
|
3/ 500 tokens
1
!condition
Code Tools

Example:

javascript
7 lines
|
33/ 500 tokens
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");
}
Code Tools

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
9 lines
|
45/ 500 tokens
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");
}
Code Tools

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
5 lines
|
20/ 500 tokens
1
2
3
4
5
let username = "";

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

Explanation:

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

Example with Truthy Values:

javascript
5 lines
|
22/ 500 tokens
1
2
3
4
5
let hasContent = "Hello, world!";

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

Explanation:

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