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 the 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 which 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 and html file.
Coding
logical.ts
class logical
{
operator(str:string):number
{
if (str == 'a' || str == "e" || str == "i" || str == "o" || str == "u")
{
return1;
}
else
{
return0;
}
}
}
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
Click on Ok.
Output 2
Output 3
If we do not enter any character or enter any string then show this result.
Output 4
Reference By
http://www.typescriptlang.org/