Sometimes we will need to run some code if at least one of two conditions is true. The logical or operator (||) returns true if either of the conditions is true. Otherwise, if both conditions are false, it returns false.
For example:
7 >= 10 || 10 < 12 // Evaluates to true
We have two conditions separated by ||
operator:
7 >= 10
, which evaluates to false10 < 12
, which evaluates to trueAnother example:
10 < 15 || 10 > 9 // Evaluates to true as both conditions are true
An example inside an if
statement:
let x = 10;
if(x != 10 || 12 < x) { // Evaluates to false
console.log("This is true!");
}
Both conditions evalute to false, so the entire condition evaluates to false and the program prints nothing.
Assignment
Follow the Coding Tutorial and play with the or operator.
Hint
Look at the examples above if you get stuck.
In this lesson, we will explore the logical or operator (||) in JavaScript. This operator is crucial for making decisions in your code based on multiple conditions. Understanding how to use the or operator effectively can help you write more flexible and powerful programs.
The logical or operator (||) is used to combine two conditions. It returns true if at least one of the conditions is true. If both conditions are false, it returns false. This operator is particularly useful in scenarios where you want to execute a block of code if any one of several conditions is met.
For example:
let a = 5;
let b = 10;
if (a > 3 || b < 5) {
console.log("At least one condition is true");
}
In this example, the first condition a > 3
is true, so the entire expression evaluates to true, and the message is logged to the console.
Let's break down the key concepts and techniques involved in using the or operator (||):
Example:
let x = 10;
let y = 20;
if (x > 5 || y < 15) {
console.log("Condition met");
}
In this example, the first condition x > 5
is true, so the second condition y < 15
is not evaluated, and the message is logged to the console.
Let's look at some examples to see how the or operator (||) can be used in different contexts:
let age = 18;
let hasPermission = true;
if (age >= 18 || hasPermission) {
console.log("Access granted");
}
In this example, the condition age >= 18
is true, so the message "Access granted" is logged to the console.
When using the or operator (||), be mindful of the following common pitfalls:
Best practices:
For more advanced use cases, you can combine the or operator (||) with other logical operators like and operator (&&):
let a = 5;
let b = 10;
let c = 15;
if ((a > 3 || b < 5) && c > 10) {
console.log("Complex condition met");
}
In this example, the expression (a > 3 || b < 5)
evaluates to true, and c > 10
is also true, so the message "Complex condition met" is logged to the console.
Here is a well-commented code snippet demonstrating the correct use of the or operator (||):
// Define two variables
let temperature = 30;
let isRaining = false;
// Check if it's either hot or raining
if (temperature > 25 || isRaining) {
console.log("Stay indoors");
} else {
console.log("Go outside and enjoy the weather");
}
In this example, the condition temperature > 25
is true, so the message "Stay indoors" is logged to the console.
When debugging code that uses the or operator (||), consider the following tips:
console.log
statements to print the values of your variables and conditions.Example of a test case:
function canEnterClub(age, hasInvitation) {
return age >= 18 || hasInvitation;
}
// Test cases
console.log(canEnterClub(20, false)); // true
console.log(canEnterClub(16, true)); // true
console.log(canEnterClub(16, false)); // false
When approaching problems that involve the or operator (||), consider the following strategies:
In this lesson, we covered the logical or operator (||) in JavaScript. We discussed its significance, fundamental concepts, common pitfalls, and best practices. By mastering the or operator, you can write more flexible and powerful code. Keep practicing and exploring different use cases to strengthen your understanding.
For further reading and practice, consider the following resources:
Our interactive tutorials and AI-assisted learning will help you master problem-solving skills and teach you the algorithms to know for coding interviews.
Start Coding for FREE