Introduction
In this chapter, we will explore comparison operators in TypeScript. Comparison operators are used to compare two values and return a boolean result (true or false).
Table of Contents
- Definition
- Types of Comparison Operators
- Equal to (
==
) - Not equal to (
!=
) - Strict equal to (
===
) - Strict not equal to (
!==
) - Greater than (
>
) - Less than (
<
) - Greater than or equal to (
>=
) - Less than or equal to (
<=
)
- Equal to (
- Examples and Output
- Conclusion
Definition
Comparison operators in TypeScript are symbols used to compare two values and return a boolean result. These operators are commonly used in conditional statements and loops to control the flow of the program based on certain conditions.
Types of Comparison Operators
Equal to (==
)
Definition
The equal to operator compares two values for equality.
Syntax
let result = operand1 == operand2;
Example
This example demonstrates the use of the equal to operator.
let a: number = 10;
let b: number = 10;
let isEqual = a == b;
console.log(isEqual);
Output
true
Not equal to (!=
)
Definition
The not equal to operator compares two values for inequality.
Syntax
let result = operand1 != operand2;
Example
This example demonstrates the use of the not equal to operator.
let a: number = 10;
let b: number = 5;
let isNotEqual = a != b;
console.log(isNotEqual);
Output
true
Strict equal to (===
)
Definition
The strict equal to operator compares two values for equality and checks their types.
Syntax
let result = operand1 === operand2;
Example
This example demonstrates the use of the strict equal to operator.
let a: number = 10;
let b: any = "10";
let isStrictEqual = a === b;
console.log(isStrictEqual);
Output
false
Strict not equal to (!==
)
Definition
The strict not equal to operator compares two values for inequality and checks their types.
Syntax
let result = operand1 !== operand2;
Example
This example demonstrates the use of the strict not equal to operator.
let a: number = 10;
let b: any = "10";
let isStrictNotEqual = a !== b;
console.log(isStrictNotEqual);
Output
true
Greater than (>
)
Definition
The greater than operator checks if the left operand is greater than the right operand.
Syntax
let result = operand1 > operand2;
Example
This example demonstrates the use of the greater than operator.
let a: number = 10;
let b: number = 5;
let isGreater = a > b;
console.log(isGreater);
Output
true
Less than (<
)
Definition
The less than operator checks if the left operand is less than the right operand.
Syntax
let result = operand1 < operand2;
Example
This example demonstrates the use of the less than operator.
let a: number = 10;
let b: number = 5;
let isLess = a < b;
console.log(isLess);
Output
false
Greater than or equal to (>=
)
Definition
The greater than or equal to operator checks if the left operand is greater than or equal to the right operand.
Syntax
let result = operand1 >= operand2;
Example
This example demonstrates the use of the greater than or equal to operator.
let a: number = 10;
let b: number = 10;
let isGreaterOrEqual = a >= b;
console.log(isGreaterOrEqual);
Output
true
Less than or equal to (<=
)
Definition
The less than or equal to operator checks if the left operand is less than or equal to the right operand.
Syntax
let result = operand1 <= operand2;
Example
This example demonstrates the use of the less than or equal to operator.
let a: number = 10;
let b: number = 15;
let isLessOrEqual = a <= b;
console.log(isLessOrEqual);
Output
true
Examples and Output
Example 1: Using Comparison Operators
In this example, we will use all the comparison operators to compare different values.
TypeScript Code (src/index.ts
)
let a: number = 10;
let b: number = 5;
let c: any = "10";
// Equal to
let isEqual = a == c;
console.log("Equal to: " + isEqual); // Output: true
// Not equal to
let isNotEqual = a != b;
console.log("Not equal to: " + isNotEqual); // Output: true
// Strict equal to
let isStrictEqual = a === c;
console.log("Strict equal to: " + isStrictEqual); // Output: false
// Strict not equal to
let isStrictNotEqual = a !== c;
console.log("Strict not equal to: " + isStrictNotEqual); // Output: true
// Greater than
let isGreater = a > b;
console.log("Greater than: " + isGreater); // Output: true
// Less than
let isLess = a < b;
console.log("Less than: " + isLess); // Output: false
// Greater than or equal to
let isGreaterOrEqual = a >= b;
console.log("Greater than or equal to: " + isGreaterOrEqual); // Output: true
// Less than or equal to
let isLessOrEqual = a <= b;
console.log("Less than or equal to: " + isLessOrEqual); // Output: false
Compiling to JavaScript
To compile the TypeScript code to JavaScript, run the TypeScript compiler:
tsc src/index.ts
Output in JavaScript (src/index.js
)
var a = 10;
var b = 5;
var c = "10";
// Equal to
var isEqual = a == c;
console.log("Equal to: " + isEqual); // Output: true
// Not equal to
var isNotEqual = a != b;
console.log("Not equal to: " + isNotEqual); // Output: true
// Strict equal to
var isStrictEqual = a === c;
console.log("Strict equal to: " + isStrictEqual); // Output: false
// Strict not equal to
var isStrictNotEqual = a !== c;
console.log("Strict not equal to: " + isStrictNotEqual); // Output: true
// Greater than
var isGreater = a > b;
console.log("Greater than: " + isGreater); // Output: true
// Less than
var isLess = a < b;
console.log("Less than: " + isLess); // Output: false
// Greater than or equal to
var isGreaterOrEqual = a >= b;
console.log("Greater than or equal to: " + isGreaterOrEqual); // Output: true
// Less than or equal to
var isLessOrEqual = a <= b;
console.log("Less than or equal to: " + isLessOrEqual); // Output: false
Running the JavaScript
To see the output of the compiled JavaScript code, run the JavaScript file using Node.js:
node src/index.js
Conclusion
In this chapter, we covered comparison operators in TypeScript, including equal to, not equal to, strict equal to, strict not equal to, greater than, less than, greater than or equal to, and less than or equal to operators. We provided examples with their outputs to illustrate how these operators work in TypeScript.
Comments
Post a Comment
Leave Comment