Javascript Conditional AND and OR



The && operator performs the conditinal AND operation. It compbines two boolean values and the result is true if both the operands are true. The conditional OR || operator returns true if either of the two operands are true.

Consider a practical example , where we check a patient's seriousness by checking his weight and blood sugar level. An excessive body weigh and high blood sugar level triggers serious health condition and he must see a doctor.

  1. <html>
  2. <body>
  3. <script type="text/javascript">
  4. <!--
  5. /*
  6. ********************************************************
  7. Example conditional AND &&
  8. ReferenceDesigner.com javascript tutorial
  9. ********************************************************
  10. */
  11.  
  12. var bodyweight = 250; // Body Weight in pounds
  13. var fastingglucose = 140; // Fasting Glucose level
  14.  
  15. if ( (bodyweight > 200) && (fastingglucose > 120) )
  16. document.write("You are overweight and have high blood glucose. You must see doc ");
  17.  
  18. //-->
  19. </script>
  20. </body>
  21. </html>


If you compile and run this program, you will get the output as follows


You are overweight and have high blodd glucose. You must see doc




You may like to try this example online here

The || operator performs the conditinal OR operation. It evaluates two boolean values and the result is true if either one or both the operands are true. Consider a practical example , where we check a patient's Systoloc blood pressure and fasting blood sugar level. We trigger an alarm and ask the patient to see a doctor if either of the two is excessive.

  1. <html>
  2. <body>
  3. <script type="text/javascript">
  4. <!--
  5. /*
  6. ********************************************************
  7. Example conditional OR operator
  8. ReferenceDesigner.com javascript tutorial
  9. ********************************************************
  10. */
  11.  
  12. var bloodpressure = 160; // Systolic Blood Pressure
  13. var fastingglucose = 85; // Fasting Glucose level
  14.  
  15. if ( (bloodpressure > 140) ||(fastingglucose > 120) )
  16. document.write("You should see a doctor ");
  17.  
  18.  
  19. //-->
  20. </script>
  21. </body>
  22. </html>


If you compile and run this program, you will get the output as follows


You should see a doctor 




You may like to try this example online here

The NOT operator is a unary operator and take a single boolean arguments and inverts it. See the folllowing example where we have tried to print true if the patient is healthy ( depending upon both his blood pressure and blood glucose being under control).

  1. <html>
  2. <body>
  3. <script type="text/javascript">
  4. <!--
  5. /*
  6. ********************************************************
  7. Example conditional OR operator
  8. ReferenceDesigner.com javascript tutorial
  9. ********************************************************
  10. */
  11.  
  12. var bloodpressure = 160; // Systolic
  13. var fastingglucose = 85; // Fasting Glucose level
  14. if(!((bloodpressure > 140) ||(fastingglucose > 120) ))
  15.  
  16. document.write("You are healthy ");
  17.  
  18. else document.write("You are not healthy ");
  19.  
  20.  
  21. //-->
  22. </script>
  23. </body>
  24. </html>


If you compile and run this program, you will get the output as follows


You should see a doctor 




You may like to try this example online here