Logical Operators in TypeScript

Logical Operators in TypeScript

 
Logical Operators work with Boolean values. In a logical operator, if you use the AND operator, the compound expression returns true if both expressions are true. If you use the OR operator then the compound expression returns true if either is true. If you use the NOT operator, the value returns true if either expression is reversed.
 
There are three logical operators as shown in the following:
 
 Operator Description Example
&& is true if both a and b are true a&&b
|| is true if either a or b is true a||b
! is true if a is not true !a
 
How logical operators work
  • && is the logical AND operator. Both expressions with the AND operator must be true for the overall test to be true.
  • || is the logical OR operator. At least one expression with the OR operator must be true for the overall test to be true.
  • ! is the logical NOT operator. The NOT operator switches the result of the expression to the other Boolean value. For example, if an expression is true, the NOT operator returns false.
  • To override the order of precedence when two or more logical operators are used in a conditional expression, you can use parentheses.
Example
The following example shows the logical operations. In this example, I have a logical class and define an operator function in the class that performs various types of operations such as AND, OR, and NOT. Let's use the following steps.
 
Step 1
Open Visual Studio 2012 and click "File" -> "New" -> "Project...". A window is shown. Give the name of your application as "logical_operator" and then click ok.
 
Step 2
After this session, the project has been created. A new window is opened on the right side. This window is called the Solution Explorer. The Solution Explorer contains the ts file, js file, CSS file, an HTML file.
 

Coding

 
logical.ts
  1. class logical {  
  2.   operator(str: string): number {  
  3.     if (str == 'a' || str == "e" || str == "i" || str == "o" || str == "u") {  
  4.       return 1;  
  5.     } else {  
  6.       return 0;  
  7.     }  
  8.   }  
  9. }  
  10. window.onload = () => {  
  11.   var str: string;  
  12.   str = prompt("Enter a character for Checking Vowel or Not").toLowerCase();  
  13.   if (str.length != 0 && str.length == 1) {  
  14.     {  
  15.       var greeter = new logical();  
  16.       var result: number;  
  17.       result = greeter.operator(str);  
  18.       if (result == 1) {  
  19.         alert("character is Vowel");  
  20.       } else {  
  21.         alert("character is not Vowel");  
  22.       }  
  23.     }  
  24.   } else {  
  25.     alert("Enter string in right format");  
  26.   }  
  27. };   
demo.html
  1. <!DOCTYPEhtml>  
  2. <htmllang="en"  
  3.     xmlns="http://www.w3.org/1999/xhtml">  
  4.     <head>  
  5.         <metacharset="utf-8"/>  
  6.         <title>Logical Operator</title>  
  7.         <linkrel="stylesheet"href="app.css"type="text/css"/>  
  8.         <scriptsrc="app.js">  
  9.         </script>  
  10.     </head>  
  11.     <body>  
  12.         <h2>Logical Operator Example in TypeScript</h2>  
  13.         <divid="content"/>  
  14.     </body>  
  15. </html>  
app.js
  1. var logical = (function() {  
  2.   function logical() {}  
  3.   logical.prototype.operator = function(str) {  
  4.     if (str == 'a' || str == "e" || str == "i" || str == "o" || str == "u") {  
  5.       return 1;  
  6.     }   
  7.     else   
  8.     {  
  9.       return 0;  
  10.     }  
  11.   };  
  12.   return logical;  
  13. })();  
  14. window.onload = function() {  
  15.   var str;  
  16.   str = prompt("Enter a character for Checking Vowel or Not").toLowerCase();  
  17.   if (str.length != 0 && str.length == 1) {  
  18.     {  
  19.       var greeter = new logical();  
  20.       var result;  
  21.       result = greeter.operator(str);  
  22.       if (result == 1) {  
  23.         alert("character is Vowel");  
  24.       }   
  25.       else {  
  26.         alert("character is not Vowel");  
  27.       }  
  28.     }  
  29.   }   
  30.   else {  
  31.     alert("Enter string in right format");  
  32.   }  
  33. };   
Output 1
 
enter-charcter.jpg
 
Click on Ok.
 
Output 2
 
final-result.jpg
 
Output 3
If we do not enter any character or enter any string then show this result.
 
blank-input.jpg
 
Output 4
 
show-error.jpg
 
Referenced By


Similar Articles