Skip to content

JavaScript if and or | Condition

  • by

You can specify multiple conditions using AND – OR in an if statement in JavaScript. Just add them within the main bracket of the if statement.

if (id !== 1 && id !== 2 && id !== 3){... }

The && operator “short-circuits” – that is, if the left condition is false, it doesn’t bother evaluating the right one.

Similarly, the || operator short-circuits if the left condition is true. With an OR (||) the operation, if any one of the conditions is true, the result is true.

JavaScript if and or

Simple example code mix and match conditions.

<!DOCTYPE html>
<html>
<body>
  <body>
    <script>
    if ((10 > 20 && 10 > 30) || (10 > 5 && 10 > 3)) {
      console.log('✅ at least one condition is met');
    } else {
      console.log('⛔️ neither condition is met');
    }

  </script>         
</body>
</html>

Output:

JavaScript if and or Condition

Using the logical AND (&&) operator

if (10 > 5 && 5 > 3) {

  console.log('✅ all conditions are met');
} else {
  console.log('⛔️ not all conditions are met');
}

Using logical OR (||)

if (5 > 10 || 2 > 10 || 10 > 5) {
  console.log('✅ at least one condition is met');
} else {
  console.log('⛔️ neither condition is met');
}

Do comment if you have any doubts or suggestions on this Js condition statement topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading