Thursday 28 February 2013

JavaScript : Logical Operators - AND, OR, NOT


Logical Operators

The logical operators used in Javascript are listed below :
OperatorUsageDescription
Logical AND ( && )a  && bis true if both a and b are true.
Logical OR ( || )a || bis true if either a or b is true.
Logical NOT ( ! )!ais true if a is not true.

JavaScript Logical AND operator (&&)

The following conditions are true :
  • true && true
  • (20 > 5) && (5 < 20)
The following conditions are false :
  • true && false
  • (20 > 5) && (20 < 5)
  • false && true
  • (20 < 5) && (20 > 5)
  • false && false
  • (20 < 5) && (5 > 20) 
javascript logical and operator
This above pictorial helps you to understand the concept of LOGICAL ANDoperation with an analogy of taps and water.
In fig.-1 of the picture, both of the taps are closed, so the water is not flowing down. Which explains that if both of conditions are FALSE or 0, return is FALSE or 0.
In fig.-2 of the picture, one of the taps are closed, even then, the water is not flowing down. Which explains that even if any of conditions are FALSE or 0, return is FALSE or 0.
fig.-3 of the picture resembles CASE -2.
In fig.-4 of the picture, both of the taps are open, so the water is flowing down. Which explains that if both of conditions are TRUE or 1, return is TRUE or 1.
So we can conclude that if and only if, both of the conditions are TRUE or 1, LOGICAL AND operations returns TRUE or 1.

Example :

The following web document demonstrates the use of AND operator (&&).

HTML Code

  1.   <!doctype html>  
  2. <head>  
  3. <meta charset="utf-8">  
  4. <title>JavaScript logical AND operator example</title>  
  5. <meta name="description" content="This document contains an example of JavaScript logical AND operator"/>  
  6. <style>  
  7. h1 {  
  8. color:red;  
  9. border-bottom: 3px groove silver;  
  10. padding-bottom: 8px;  
  11. }  
  12. </style>  
  13. </head>  
  14. <body>  
  15. <h1>JavaScript equal operator (==) example</h1>  
  16. <form name="logical_or_test" action="javascript-logical-and-operator-example1-with-dom.html">  
  17. <input type="text" id="no" placeholder="Input a number between 5 and 10 and press tab" size="60" onchange="viewOutput();"/>  
  18. <input type="submit" value="Submit" />  
  19. </form>  
  20. <script src="javascript-logical-and-operator-example1.js"></script>  
  21. </body>  
  22. </html>  
  23.     

JS Code

  1. function viewOutput()  
  2. {  
  3. 'use strict';  
  4. var no = document.getElementById('no').value;  
  5. if( no>= 5 && no<10 )  
  6. {  
  7. var newParagraph = document.createElement("p"); //creates a new paragraph element  
  8. var newText = document.createTextNode('The number is between 5 and 10'); //creates text along with ouput to be displayed   
  9. newParagraph.appendChild(newText); //created text is appended to the paragraph element created  
  10. document.body.appendChild(newParagraph); // created paragraph and text along with output is appended to the document body  
  11. }  
  12. else  
  13. {  
  14. var newParagraph1 = document.createElement("p"); //creates a new paragraph element  
  15. var newText1 = document.createTextNode('Wrong input'); //creates text along with ouput to be displayed   
  16. newParagraph1.appendChild(newText1); //created text is appended to the paragraph element created  
  17. document.body.appendChild(newParagraph1); // created paragraph and text along with output is appended to the document body  
  18. }  
  19. }  

JavaScript Logical OR operator (||)

The following conditions are true :
  • true || true
  • (20 > 5) || (5 < 20)
  • true || false
  • (20 > 5) || (20 < 5)
  • false || true
  • (20 < 5) || (20 > 5)
The following conditions are false :
  • false || false
  • (20 < 5) || (5 > 20)
javascript logical and operator
The above pictorial helps you to understand the concept of LOGICAL ORoperation with an analogy of taps and water.
In fig.-1 of the picture, both of the taps are closed, so the water is not flowing down. Which explains that if both of conditions are FALSE or 0, return is FALSE or 0.
In fig.-2 of the picture, one of the taps are closed, and we can see that the water is flowing down. Which explains that if any of conditions are TRUE or 1, return is TRUE or 1.
fig.-3 of the picture, resembles CASE -2.
In fig.-4 of the picture, both of the taps are open, so the water is flowing down. Which explains that if both of conditions are TRUE or 1, return is TRUE or 1.
So we can conclude that in LOGICAL OR operation, if any of the conditions are true, the output is TRUE or 1.

Example :

The following web document demonstrates the use of OR operator (||).

HTML Code

  1. <!doctype html>  
  2. <head><meta charset="utf-8">  
  3. <title>JavaScript logical OR operator example</title>  
  4. <meta name="description" content="This document contains an example of JavaScript logical OR operator"/>  
  5. <style>  
  6. h1 {  
  7. color:red;  
  8. border-bottom: 3px groove silver;  
  9. padding-bottom: 8px;  
  10. }</style>  
  11. </head>  
  12. <h1>JavaScript logical OR operator example</h1>  
  13. <form name="logical_or_test" action="javascript-logical-or-operator-example1-with-dom.html">  
  14. <input type="text" id="no" placeholder="Input a number less than 5 or greater than 20 and press tab" size="60" onchange="viewOutput();"/>  
  15. <input type="submit" value="Submit" />  
  16. </form>  
  17. <script src="javascript-logical-or-operator-example1.js">  
  18. </script>  
  19. </body>  
  20. </html>  
  21.         

JS Code

  1. function viewOutput()  
  2. {  
  3. 'use strict';  
  4. var no = document.getElementById('no').value;  
  5. if( no<5 || no>20 )  
  6. {  
  7. var newParagraph = document.createElement("p"); //creates a new paragraph element  
  8. var newText = document.createTextNode('Value of input is less than 5 or greater than 20'); //creates text along with ouput to be displayed   
  9. newParagraph.appendChild(newText); //created text is appended to the paragraph element created  
  10. document.body.appendChild(newParagraph); // created paragraph and text along with output is appended to the document body  
  11. }  
  12. else  
  13. {  
  14. var newParagraph1 = document.createElement("p"); //creates a new paragraph element  
  15. var newText1 = document.createTextNode('Wrong input'); //creates text along with ouput to be displayed   
  16. newParagraph1.appendChild(newText1); //created text is appended to the paragraph element created  
  17. document.body.appendChild(newParagraph1); // created paragraph and text along with output is appended to the document body  
  18. }  
  19. }  
  20.         

JavaScript Logical NOT operator (!)

HTML Code

  1. <!doctype html>  
  2. <head>  
  3. <meta charset="utf-8">  
  4. <title>JavaScript logical NOT operator example with DOM</title>  
  5. <meta name="description" content="This document contains an example of JavaScript logical NOT operator"/>  
  6. <style>  
  7. h1 {  
  8. color:red;  
  9. border-bottom: 3px groove silver;  
  10. padding-bottom: 8px;  
  11. }  
  12. </style>  
  13. </head>  
  14. <h1>JavaScript logical NOT operator example</h1>  
  15. <script src="javascript-logical-not-operator-example1.js"></script>  
  16. </body>  
  17. </html>  
  18.         

JS Code

  1. var a = 20;  
  2. var b = 5;  
  3. var newParagraph = document.createElement("p"); //creates a new paragraph element  
  4. var newText = document.createTextNode('Value of a = '+a+' b = '+b); //creates text along with ouput to be displayed   
  5. newParagraph.appendChild(newText); //created text is appended to the paragraph element created  
  6. document.body.appendChild(newParagraph); // created paragraph and text along with output is appended to the document body  
  7. if( a != b )   
  8. {  
  9. var newParagraph1 = document.createElement("p"); //creates a new paragraph element  
  10. var newText1 = document.createTextNode('a is not equal to b [ != operator ]'); //creates text along with ouput to be displayed   
  11. newParagraph1.appendChild(newText1); //created text is appended to the paragraph element created  
  12. document.body.appendChild(newParagraph1); // created paragraph and text along with output is appended to the document body  
  13. }  
  14. else  
  15. {  
  16. var newParagraph2 = document.createElement("p"); //creates a new paragraph element  
  17. var newText2 = document.createTextNode('a is equal to b.'); //creates text along with ouput to be displayed   
  18. newParagraph2.appendChild(newText2); //created text is appended to the paragraph element created  
  19. document.body.appendChild(newParagraph2); // created paragraph and text along with output is appended to the document body  
  20. }  
  21.         

No comments:

Post a Comment