What are Relational Operators?
In TypeScript, relational operators are used to determine the type of relationship that exists between two entities.
This operator will return a boolean value, meaning they return either true or false.
List of Relational Operators
Here’s the list of Relational Operators:
- Equal to (==)
- Strictly equal to (===)
- Strictly not equal to (!==)
- Not equal to (!=)
- Greater than (>)
- Greater than or equal to (>=)
- Less than (<)
- Less than or equal to (<=)
1. Equal to (==) operator
The Equal to (==) operator returns true if the values of both operands are equal or the same.
Syntax:
output = operand1 == operand2;
Here’s the illustration:
(X == Y) is True
Let’s take a look at the example:
let x: number = 10;
let y: number = 10;
// == is the equal operator
let isEqual: boolean = (x == y);
console.log(isEqual);
Output:
true
2. Strictly equal to (===) operator
The Strictly equal to (===) operator returns true if both operand are the same with out coercion.
Syntax:
output = operand1 === operand2;
Here’s the illustration:
(X ===Y) is True
Let’s take a look at the example:
let x: number = 10;
let y: number = 20;
// === is the strict equality operator
let isEqual: boolean = (x === y);
console.log(isEqual);
Output:
false
3. Not equal to (!=) operator
The Not equal to (!=) operator returns true if the values of the two operands are not equal or the same right after type coercion.
Syntax:
output= operand1 != operand2;
Here’s the illustration:
(X != Y) is True
Let’s take a look at the example:
let x: number = 10;
let y: number = 20;
// != is the not equal operator
let isNotEqual: boolean = (x != y);
console.log(isNotEqual);
Output:
true
4. Strictly not equal to (!== ) operator
The strictly inequality operator (!==) returns true if the values of the two operands are not equal and they are not of the same type.
Syntax:
output = operand1 !== operand2;
Here’s the illustration:
(x !== y) is True
Let’s take a look at the example:
let x: number = 10;
let y: number = 20;
// !== is the strict inequality operator
let isStrictlyNotEqual: boolean = (x !== y);
console.log(isStrictlyNotEqual);
Output:
true
5. Greater than (>) operator
The Greater than (>) operator returns true if the value of the left operand is greater than the value of the right operand.
Syntax:
output = operand1 > operand2;
Here’s the illustration:
(X > Y) is False
Let’s take a look at the example:
let x: number = 10;
let y: number = 20;
// > is the greater than operator
let isGreaterThan: boolean = (x > y);
console.log(isGreaterThan);
Outputs:
false
6. Greater than or equal to (>=) operator
The Greater than or equal to (>=) operator returns true if the value in the left operand is greater than or if it is equal to the value in the right operand.
Syntax:
output = operand1 >= operand2;
Here’s the illustration:
(X >= Y) is False
Let’s take a look at the example:
let x: number = 10;
let y: number = 10;
// >= is the greater than or equal to operator
let isGreaterThanOrEqual: boolean = (x >= y);
console.log(isGreaterThanOrEqual);
Output:
true
7. Less Than (<) operator
The Less Than (<) operator returns true if the value in the left operand is less than the value in the right operand.
Syntax:
output = operand1 < operand2;
Here’s the Illustration:
(X < Y) is False
Let’s take a look at the example:
let x: number = 20;
let y: number = 10;
// < is the less than operator
let isLessThan: boolean = (x < y);
console.log(isLessThan);
Outputs:
false
8. Less than or equal to (<=) operator
The Less than or equal to (<=) operator returns true if the value in the left operand is less than or if it is equal in the value of the right operand.
Syntax:
output = operand1 <= operand2;
Here’s the illustration:
(X <= Y); False
Let’s take a look at the example:
let x: number = 20;
let y: number = 10;
// <= is the less than or equal to operator
let isLessThanOrEqual: boolean = (x <= y);
console.log(isLessThanOrEqual);
Output:
false
Conclusion
In conclusion, the relational operators in TypeScript are used when comparing two values to determine the relationship that exists between two entities.
The relational operators will return a boolean value, meaning they return either true or false.
I hope this article has provided you with insights and helped you understand the TypeScript relational operators.
If you have any questions or inquiries, please don’t hesitate to leave a comment below.