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.
- As the following three logical operators demonstrate:
![](https://www.thecodehubs.com/wp-content/uploads/2022/11/2022-11-25_10h04_56-300x35.png)
- 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.
- 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.”
- 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.
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");
}
};
<!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>
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");
}
};
- Click on Ok.
![](https://www.thecodehubs.com/wp-content/uploads/2022/11/2022-11-25_10h18_06-300x156.png)
![](https://www.thecodehubs.com/wp-content/uploads/2022/11/2022-11-25_10h20_53-300x148.png)