Skip to content

3 conditions in if statement JavaScript | Example code

  • by

Using either “&&” or “||” i.e. logical AND or logical OR operator or combination of can achieve 3 conditions in if statement JavaScript.

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.

Read more: JavaScript if multiple conditions

Example 3 conditions in if statement JavaScript

Simple example code will test for all 3 of those conditions or, after seeing that b does equal 1, or c is equal to 1.

<!DOCTYPE html>
<html>
<body>
  <script>
   a = 1;
   b = 2;
   c = 1;

   if (a==1 && b==2 && c==1){
    console.log("ALL")
  };

  if (a==1 && (b==1 || c==1)){
    console.log("a and b or c")
  };

</script>

</body>
</html>

Output:

3 conditions in if statement JavaScript

You can combine as many logic operations as you want into one if a statement using any of the javascript logic and comparison operators.

if ((intervalCheck == 0) || (intervalCheck >= 5000 && intervalCheck <= 3600000)) {
    // put your code here
}

Do comment if you have any doubts or suggestions on this JS if code.

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