Note: If you're new to TypeScript, check our Getting Started with TypeScript tutorial first.
TypeScript Comparison Operators
Comparison operators compare two values and return a boolean result (true
or false
). For example,
const a: number = 3, b: number = 2;
console.log(a > b);
// Output: true
Here, we have used the >
comparison operator to check whether a (whose value is 3) is greater than b (whose value is 2).
Since 3 is greater than 2, we get true
as output.
Note: In the above example, a > b
is called a boolean expression since evaluating it results in a boolean value.
Commonly Used Comparison Operators
Operator | Meaning | Example |
---|---|---|
== |
Equal to | 3 == 5 (Output: false ) |
!= |
Not equal to | 3 != 4 (Output: true ) |
=== |
Strictly equal to | 3 === "3" (Output: false ) |
!== |
Strictly not equal to | 3 !== "3" (Output: true ) |
> |
Greater than | 4 > 4 (Output: false ) |
< |
Less than | 3 < 3 (Output: false ) |
>= |
Greater than or equal to | 4 >= 4 (Output: true ) |
<= |
Less than or equal to | 3 <= 3 (Output: true ) |
1. TypeScript Equal To Operator
The equal to operator ==
evaluates to
true
- If the values of the operands are equal.false
- If the values of the operands are not equal.
For example,
const greeting1: string = "hello";
const greeting2: string = "Hello";
// Comparing different values of string type
console.log(greeting1 == greeting2); // false
// Comparing a string variable with a string literal
console.log(greeting1 == "hello"); // true
const myAge: number = 25;
const yourAge: number = 25;
// Comparing identical values of number type
console.log(myAge == yourAge); // true
// Comparing a number variable with a numerical literal
console.log(myAge == 36); // false
Here's how this program works:
Expression | Result | Reason |
---|---|---|
greeting1 == greeting2 | false |
greeting1 and greeting2 have different values. |
myAge == yourAge | true |
myAge and yourAge have the same value. |
Note: In TypeScript, ==
is a comparison operator, whereas =
is an assignment operator. If you mistakenly use =
instead of ==
, you might get unexpected results.
Frequently Asked Questions
Unlike JavaScript, TypeScript will throw an error if you try to compare data of different types. This is because TypeScript enforces strict type checking by default. For example,
const greeting: string = "hello";
const age: number = 25;
// Comparing string and number results in error
console.log(greeting == age);
TypeScript also treats literals as their own types.
This means that using equality operators like ==
, !=
, ===
, and !==
with two different literals will result in a type error.
However, using these operators with two literals of the same type and value won't result in an error.
For example,
// Comparing two numerical literals that are identical in value
// This is a valid code
console.log(2 == 2); // true
// Comparing a numerical literal with a string literal
// Gives an error because you can't compare different types
console.log(2 == "2"); // Error
// Comparing two numerical literals with different values
// Gives an error because they are considered different types
console.log(2 == 3); // Error
Note: Relational operators like >
, >=
, <
, and <=
allow you to compare literals of the same type but different values. For example, expressions like 3 > 6
or 2 <= 9
won't cause errors. But 3 == 6
or 2 != 9
will cause errors.
It's easy to see why you can't compare the numerical literal 2 with the string literal "2"
. But why is 2 == 2
valid but 2 == 3
is not?
The reason is that TypeScript considers the literal 2
as a data type like number
or string
.
More specifically, TypeScript treats the literal 2
as a subset of the number
type that can only have a single value (2) and nothing else.
Similarly, the literals "2"
and 3
are considered as their own data types, where "2"
is a subset of string
type and 3
is a subset of number
type.
Thus, this is how TypeScript interprets our comparisons:
2 == 2
- Comparing data type2
with data type2
(doesn't cause errors).2 == "2"
- Comparing data type2
with data type"2"
(causes errors).2 == 3
- Using data type2
with data type3
(causes errors).
2. Not Equal To Operator
The not equal to operator !=
evaluates to
true
- If the values of the operands aren't equal.false
- If the values of the operands are equal.
For example,
const myAge: number = 25;
const yourAge: number = 25;
const greeting1: string = "hello";
const greeting2: string = "Hello";
// Comparing variables of same type and different values
console.log(greeting1 != greeting2); // true
// Comparing variables of same type and same values
console.log(myAge != yourAge); // false
// Comparing string variable with string literal
console.log(greeting1 != "hello"); // false
// Comparing number variable with numerical literal
console.log(myAge != 36); // true
// Comparing literals of same type and value
console.log(2 != 2); // false
Here's how this program works:
Expression | Result | Reason |
---|---|---|
greeting1 != greeting2 | true |
greeting1 and greeting2 have different values. |
myAge != yourAge | false |
myAge and yourAge have the same value. |
greeting1 != "hello" | false |
The value of greeting1 is "hello" . |
myAge != 36 | true |
The value of myAge is initially 25, which is not the same value as the numerical literal 36. |
2 != 2 | false |
Both literals are of type 2 (TypeScript considers literals as their own types). |
Unsafe Comparisons in TypeScript
As you know, there are many unsafe comparisons that are acceptable in JavaScript but might throw errors in TypeScript. Here are a few examples:
// Comparing variables of different types
// console.log(greeting1 != myAge); // Error
// Comparing variable of one type with literal of another type
// console.log(myAge != "36"); // Error
// Comparing literals of different types
// console.log(2 != "2"); // Error
// Comparing numerical literals with different values
// console.log(2 != 3); // Error
Here's why this program doesn't work:
Expression | Result | Reason |
---|---|---|
greeting1 != myAge | Error | greeting1 is of string type but myAge is of number type. |
myAge != "36" | Error | Comparison between a number variable and a string literal. |
2 != "2" | Error | Comparison between literals of type 2 and type "2" . |
2 != "3" | Error | Comparison between literals of type 2 and type 3 . |
3. Strict Equal To Operator
The strict equal to operator ===
evaluates to
true
- If both the values and the types of the operands are the same.false
- If either the values or the types of the operands are not the same.
Since TypeScript forbids comparison of different types by default, we'll only look at valid examples:
const myAge: number = 25;
const yourAge: number = 25;
const stringAge: string = "25";
// Comparing identical values of number type
console.log(myAge === yourAge); // true
// Comparing a number variable and a numerical literal
console.log(myAge === 36); // false
// Converting stringAge to number and comparing it with myAge
// Valid code because we're comparing two number values
console.log(myAge === Number(stringAge)); // true
4. Strict Not Equal To Operator
The strict not equal to operator !==
evaluates to
true
- If either the values or the types of the operands are not the same.false
- If both the values and the types of the operands are the same.
const myAge: number = 25;
const yourAge: number = 25;
const stringAge: string = "25";
console.log(myAge !== yourAge); // false
console.log(myAge !== 36); // true
console.log(myAge !== Number(stringAge)); // false
5. Greater Than Operator
The greater than operator >
returns
true
- If the value on the left is greater than the value on the right.false
- If the value on the left isn't greater than the value on the right.
For example,
const myAge: number = 48;
const yourAge: number = 25;
// Left operand is greater
console.log(myAge > yourAge); // true
// Both operands are equal
console.log(myAge > 48); // false
// Left operand is smaller
console.log(36 > 48); // false
6. Greater Than Or Equal To Operator
The greater than or equal to operator >=
returns
true
- If the value on the left is greater than or equal to the value on the right.false
- If the value on the left is less than the value on the right.
For example,
const myAge: number = 48;
const yourAge: number = 25;
// Left operand is greater
console.log(myAge >= yourAge); // true
// Both operands are equal
console.log(myAge >= 48); // true
// Left operand is smaller
console.log(36 >= 48); // false
7. Less Than Operator
The less than operator <
returns
true
- If the value on the left is less than the value on the right.false
- If the value on the left isn't less than the value on the right.
For example,
const myAge: number = 48;
const yourAge: number = 25;
// Left operand is smaller
console.log(yourAge < myAge); // true
// Both operands are equal
console.log(myAge < 48); // false
// Left operand is greater
console.log(48 < 36); // false
8. Less Than Or Equal To Operator
The less than or equal to operator <=
returns
true
- If the value on the left is less than or equal to the value on the right.false
- If the value on the left is greater than the value on the right.
For example,
const myAge: number = 48;
const yourAge: number = 25;
// Left operand is smaller
console.log(yourAge <= myAge); // true
// Both operands are equal
console.log(myAge <= 48); // true
// Left operand is greater
console.log(48 <= 36); // false
TypeScript Logical Operators
Logical operators return a boolean value by evaluating boolean expressions. For example,
const x: number = 5;
const y: number = 3;
console.log((x < 6) && (y < 5));
// Output: true
Here, &&
is the logical operator AND. Since both the boolean expressions x < 6
and y < 5
are true
, evaluating them with the &&
operator also results in true
.
Commonly Used Logical Operators
Operator | Syntax | Description |
---|---|---|
&& (Logical AND) |
expression1 && expression2 |
true only if both expression1 and expression2 are true |
|| (Logical OR) |
expression1 || expression2 |
true if either expression1 or expression2 is true |
! (Logical NOT) |
!expression |
false if expression is true and vice versa |
1. Logical AND Operator
The logical AND operator &&
returns true
if both the expressions are true
. For example,
let x: number = 2;
// Both expressions are true
console.log((x < 4) && (3 >= x)); // true
// Only one expression is true
console.log((x <= 4) && (x == 3)); // false
// Both expressions are false
console.log((x > 4) && (x == 3)); // false
Here,
(x < 4) && (3 >= x)
results intrue
because both expressions aretrue
.(x <= 4) && (x == 3)
results infalse
because the expressionx == 3
isfalse
.(x > 4) && (x == 3)
results infalse
because both expressions arefalse
.
2. Logical OR Operator
The logical OR operator ||
returns true
if at least one expression is true
. For example,
let x: number = 2;
// Both expressions are true
console.log((x < 4) || (3 >= x)); // true
// Only one expression is true
console.log((x <= 4) || (x == 3)); // true
// Both expressions are false
console.log((x > 4) || (x == 3)); // false
Here,
(x < 4) || (3 >= x)
results intrue
because both expressions aretrue
.(x <= 4) || (x == 3)
results intrue
because the expressionx <= 4
istrue
.(x > 4) || (x == 3)
results infalse
because both expressions arefalse
.
3. Logical NOT Operator
The logical NOT operator !
returns true
if the specified expression is false
and vice versa. For example,
let x: number = 2;
// NOT on true
console.log(!true); // false
// NOT on false
console.log(!false); // true
// Comparison example
console.log(!(x < 3)); // false
Here,
!true
results infalse
because!
inverts the value oftrue
tofalse
.!false
results intrue
because!
inverts the value offalse
totrue
.!(x < 3)
results infalse
because!
inverts thetrue
value of(x < 3)
tofalse
.
Frequently Asked Question
In TypeScript, we use comparison operators to compare two values and find the resulting boolean value (true
or false
). For example,
// Less than operator
console.log(4 < 5);
// Output: true
In the above example, we used the <
operator to find the boolean value for the condition 4 < 5
.
On the other hand, we use logical operators to perform logical operations on boolean expressions. For example,
// ! logical NOT
console.log(!(4 < 5));
// Output: false
Here, the expression 4 < 5
gives us the boolean value true
. The !
operator then acts on this boolean value and inverts it to false
.
Also Read: