How to use OR condition in a JavaScript IF statement?

9    Asked by kerry_2982 in Java , Asked on Sep 14, 2025

How do you use the OR condition in a JavaScript if statement, and what does it mean for decision-making?  By using the logical OR operator (||), you can check multiple conditions and execute code if at least one of them is true.

Answered by Joseph Gilmore

In JavaScript, the OR condition in an if statement is written using the logical OR operator ||. This operator allows you to test multiple conditions at once and execute a block of code if at least one condition evaluates to true. It’s very useful when you want flexibility in decision-making without writing multiple separate if statements.

Here’s how it works:

Basic syntax:

 if (condition1 || condition2) {
    // code runs if either condition1 OR condition2 is true
}

  • At least one true is enough: If the first condition is false but the second is true, the code inside the if block will still run.
  • Short-circuiting: JavaScript stops evaluating as soon as it finds a true condition, making the code slightly more efficient.
  • Multiple ORs: You can chain more than two conditions, e.g., (a || b || c).

Example:

let age = 20;
let hasPermission = true;
if (age >= 18 || hasPermission) {
  console.log("Access granted!");
} else {
  console.log("Access denied!");
}

Here, the message prints "Access granted!" because at least one condition is true.

Key points to remember:

  • Use || when any one of the conditions should be enough to run the code.
  • Combine it with && (AND) for more complex logic.
  • Be careful with truthy and falsy values, as JavaScript may evaluate them differently than you expect.



Your Answer

Interviews

Parent Categories