Logical Operators in Angular TypeScript

Logical Operators in TypeScript

  • Boolean values are used by logical operators. In a logical operator, the compound expression returns true if both expressions are true if the AND operator is used. The compound expression returns true if one of the two conditions is true if the OR operator is used. If either expression is inverted when the NOT operator is used, the value returns true.
  1. As the following three logical operators demonstrate:

  • 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.
  1. Step 1
  • Click “File” -> “New” -> “Project…” in Visual Studio 2012 after it has opened. A window is shown. Select “logical operator” as the name of your programme, and then click “ok.”
  1. Step 2
  • This session has resulted in the creation of the project. The right side of the screen displays a new window. The Solution Explorer is the name of this window. The JS, CSS, and HTML files are all present in the Solution Explorer.
  • logical.ts
class logical {  
  operator(str: string): number {  
    if (str == 'a' || str == "e" || str == "i" || str == "o" || str == "u") {  
      return 1;  
    } else {  
      return 0;  
    }  
  }  
}  
window.onload = () => {  
  var str: string;  
  str = prompt("Enter a character for Checking Vowel or Not").toLowerCase();  
  if (str.length != 0 && str.length == 1) {  
    {  
      var greeter = new logical();  
      var result: number;  
      result = greeter.operator(str);  
      if (result == 1) {  
        alert("character is Vowel");  
      } else {  
        alert("character is not Vowel");  
      }  
    }  
  } else {  
    alert("Enter string in right format");  
  }  
};
  • demo.html
<!DOCTYPEhtml>  
<htmllang="en"  
    xmlns="http://www.w3.org/1999/xhtml">  
    <head>  
        <metacharset="utf-8"/>  
        <title>Logical Operator</title>  
        <linkrel="stylesheet"href="app.css"type="text/css"/>  
        <scriptsrc="app.js">  
        </script>  
    </head>  
    <body>  
        <h2>Logical Operator Example in TypeScript</h2>  
        <divid="content"/>  
    </body>  
</html>
  • app.js
var logical = (function() {  
  function logical() {}  
  logical.prototype.operator = function(str) {  
    if (str == 'a' || str == "e" || str == "i" || str == "o" || str == "u") {  
      return 1;  
    }   
    else   
    {  
      return 0;  
    }  
  };  
  return logical;  
})();  
window.onload = function() {  
  var str;  
  str = prompt("Enter a character for Checking Vowel or Not").toLowerCase();  
  if (str.length != 0 && str.length == 1) {  
    {  
      var greeter = new logical();  
      var result;  
      result = greeter.operator(str);  
      if (result == 1) {  
        alert("character is Vowel");  
      }   
      else {  
        alert("character is not Vowel");  
      }  
    }  
  }   
  else {  
    alert("Enter string in right format");  
  }  
};
  • Output 1
  1. Click on Ok.

  • Output 2

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories