Note: If you're new to TypeScript, check our Getting Started with TypeScript tutorial first.
In TypeScript, an operator is a special symbol that checks, changes, or combines operands (values). For example,
let result: number = 2 + 3;
console.log(result);
// Output: 5
Here, we've used the +
operator to add two operands, 2 and 3.
Types of Operators
TypeScript operators are categorised based on their functionalities. Some of them are:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Ternary Operator
- Type (typeof) Operator
Let's learn about them in detail.
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations. For example,
let product: number = 2 * 4;
console.log(product);
// Output: 8
Here, we've used the *
arithmetic operator to calculate the product of 2 and 4.
Some commonly used arithmetic operators are:
Operator | Name | Example |
---|---|---|
+ |
Addition | 3 + 4 (Results in 7) |
- |
Subtraction | 5 - 2 (Results in 3) |
* |
Multiplication | 2 * 3 (Results in 6) |
/ |
Division | 4 / 2 (Results in 2) |
% |
Remainder | 5 % 2 (Results in 1) |
++ |
Increment | let num: number = 5; ++num; // num == 6 |
-- |
Decrement | let num: number = 5; num--; // num == 4 |
** |
Exponentiation | 4 ** 2 (Results in 16) |
Example 1: Arithmetic Operators
Let's explore an example to see how various arithmetic operators work.
let num: number = 10;
// Division: divides num by 3
let divisionResult: number = num / 3;
console.log(divisionResult); // Output: 3.3333333333333335
// Remainder: the remainder when num is divided by 3
let remainderResult: number = num % 3;
console.log(remainderResult); // Output: 1
// Increment: increases num by 1
num++;
console.log(num); // Output: 11
// Decrement: decreases num by 1
num--;
console.log(num); // Output: 10
// Exponentiation: raises num to the power of 2
let exponentiationResult: number = num ** 2;
console.log(exponentiationResult); // Output: 100
Assignment Operators
The assignment operator is used to assign values to a variable. For example,
let num: number = 7;
console.log(num);
// Output: 7
Here, we've used the =
operator to assign the value 7 to the variable num
.
Some commonly used assignment operators in TypeScript are:
Operator | Name | Example |
---|---|---|
= |
Assignment Operator | let a: number = 7; (Value of a is 7) |
+= |
Addition Assignment | a += 5; (Same as a = a + 5; ) |
-= |
Subtraction Assignment | a -= 2; (Same as a = a - 2; ) |
*= |
Multiplication Assignment | a *= 3; (Same as a = a * 3; ) |
/= |
Division Assignment | a /= 2; (Same as a = a / 2; ) |
%= |
Remainder Assignment | a %= 4; (Same as a = a % 4; ) |
**= |
Exponentiation Assignment | a **= 2; (Same as a = a ** 2; ) |
Example 2: Assignment Operators
Let's explore an example to see how various assignment operators work:
let num: number = 7;
// Using Addition Assignment
num += 5;
console.log("After += 5:", num);
// Using Subtraction Assignment
num -= 2;
console.log("After -= 2:", num);
// Using Multiplication Assignment
num *= 3;
console.log("After *= 3:", num);
// Using Division Assignment
num /= 2;
console.log("After /= 2:", num);
// Using Remainder Assignment
num %= 4;
console.log("After %= 4:", num);
// Using Exponentiation Assignment
num **= 2;
console.log("After **= 2:", num);
Output
After += 5: 12 After -= 2: 10 After *= 3: 30 After /= 2: 15 After %= 4: 3 After **= 2: 9
Comparison Operators
The comparison operators compare two values and return a boolean value (true
or false
) based on the comparison result. For example,
let num1: number = 5;
let num2: number = 3;
// Check if num1 is greater than num2
let isGreater: boolean = num1 > num2;
console.log(isGreater);
// Output: true
Here, we've used the greater than (>
) comparison operator to compare num1
and num2
. Since 5 is greater than 3, the expression num1 > num2
evaluates to true
.
Some commonly used comparison operators are:
Operator | Name | Example |
---|---|---|
== |
Equal to | 3 == 5 (Output: false ) |
!= |
Not equal to | 3 != 4 (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 ) |
=== |
Strictly equal to | 3 === "3" (Output: false ) |
!== |
Strictly not equal to | 3 !== "3" (Output: true ) |
Example 3: Comparison Operators
let a: number = 5;
let b: number = 10;
// Equal to
console.log(a == 5); // true
console.log(a == b); // false
// Strictly equal to
console.log(a === 5); // true
// Not equal to
console.log(a != b); // true
// Strictly not equal to
console.log(a !== 10); // true
// Greater than
console.log(b > a); // true
// Less than
console.log(b < a); // false
// Greater than or equal to
console.log(a >= 5); // true
// Less than or equal to
console.log(b <= 10); // true
Logical Operators
These operators perform logical operations, typically between boolean values. Logical operators are fundamental for making decisions based on multiple conditions. For example,
let isLoggedIn: boolean = true;
let hasAdminRights: boolean = false;
// Check if the user is logged in and has admin rights
let canAccessDashboard: boolean = isLoggedIn && hasAdminRights;
console.log(canAccessDashboard);
// Output: false
Here, we've used the logical AND operator (&&
) to determine if the user can access the admin dashboard.
The combined condition is evaluated as false
since isLoggedIn
is true
but hasAdminRights
is false
.
The commonly used logical operators are:
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 |
Example 4: Logical Operators
Let's explore an example to see how various logical operators work:
// Logical AND example
let hasPassword = true;
let hasUsername = true;
let canLogin = hasPassword && hasUsername;
console.log("Can login:", canLogin);
// Logical OR example
let isWeekday = false;
let isHoliday = true;
let canRelax = isWeekday || isHoliday;
console.log("Can relax:", canRelax);
// Logical NOT example
let isBusy = false;
let isFree = !isBusy;
console.log("Is free:", isFree);
Here,
Expression | Result | Reason |
---|---|---|
hasPassword && hasUsername |
true |
Both hasPassword and hasUsername are true . |
isWeekday || isHoliday |
true |
isWeekday is false and isHoliday is true . |
!isBusy |
true |
isBusy is false , and the NOT operator inverts the false to true . |
Ternary Operator
The ternary operator in TypeScript, also known as the conditional operator, consists of three parts:
- A condition.
- An expression to execute if the condition is
true
. - An expression to execute if the condition is
false
.
The syntax of the ternary operator is:
condition ? expression1 : expression2
Let's see a simple example of the ternary operator.
let age: number = 18;
let canVote: string = age >= 18 ? "Yes, you can vote." : "No, you cannot vote.";
console.log(canVote);
Output
Yes, you can vote.
Here,
- Condition:
age >= 18
checks ifage
is 18 or greater. - Expression1:
"Yes, you can vote."
is returned if the condition istrue
. - Expression2:
"No, you cannot vote."
is returned if the condition isfalse
.
Note: The ternary operator is a shorthand form of the if...else
statement, which you will learn about in the upcoming articles.
typeof Operator
The typeof
operator determines the type of a variable or an expression. For example,
let greetings = "Hello, TypeScript!";
let typeOfgreetings = typeof greetings; // Returns 'string'
console.log(typeOfgreetings);
Output
string
Here, typeof greetings
returns "string"
because greetings
is initialized with a string data.