TypeScript Operators
Operators are symbols or keywords that perform operations on variables and values in TypeScript. They help us manipulate data, compare values, and control the flow of logic in our programs.
Types of Operators in TypeScript
- Arithmetic Operators
- Logical Operators
- Relational Operators
- Bitwise Operators
- Assignment Operators
- Ternary (Conditional) Operator
- Type Operators
Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations on numbers.
Operator | Description | Example | Result |
---|---|---|---|
+ | Adds two numbers. The result is their sum. | 5 + 3 | 8 |
- | Subtracts the second number from the first. The result is their difference. | 5 - 3 | 2 |
* | Multiplies two numbers. The result is their product. | 5 * 3 | 15 |
/ | Divides the first number by the second. The result is their quotient. | 6 / 3 | 2 |
% | Finds the remainder when the first number is divided by the second. | 5 % 3 | 2 |
++ | Increments the value of a variable by one. | let a = 5; a++ | 6 |
-- | Decrements the value of a variable by one. | let a = 5; a-- | 4 |
Logical Operators
Logical operators are used to combine or invert conditions.
Operator | Description | Example | Result |
---|---|---|---|
&& | Returns true if both conditions are true . Otherwise, it returns false . | true && false | false |
││ | Returns true if at least one condition is true . Otherwise, it returns false . | true ││ false | true |
! | Reverses the value of a condition. If it's true , it becomes false , and vice versa. | !true | false |
Relational Operators
Relational operators compare two values and return a boolean (true
or false
).
Operator | Description | Example | Result |
---|---|---|---|
== | Checks if two values are equal. It doesn't consider data types. | 5 == 5 | true |
!= | Checks if two values are not equal. It doesn't consider data types. | 5 != 3 | true |
> | Checks if the first value is greater than the second. | 5 > 3 | true |
< | Checks if the first value is less than the second. | 5 < 3 | false |
>= | Checks if the first value is greater than or equal to the second. | 5 >= 5 | true |
<= | Checks if the first value is less than or equal to the second. | 5 <= 3 | false |
Bitwise Operators
Bitwise operators perform operations at the binary level.
Operator | Description | Example | Result |
---|---|---|---|
& | Performs a bitwise AND operation. Sets a bit to 1 if both corresponding bits are 1 . | 5 & 3 | 1 |
│ | Performs a bitwise OR operation. Sets a bit to 1 if at least one corresponding bit is 1 . | 5 │ 3 | 7 |
^ | Performs a bitwise XOR operation. Sets a bit to 1 if only one of the corresponding bits is 1 . | 5 ^ 3 | 6 |
~ | Inverts all bits in a number (bitwise NOT). | ~5 | -6 |
<< | Shifts the bits of the first operand to the left by the specified number of places. | 5 << 1 | 10 |
>> | Shifts the bits of the first operand to the right by the specified number of places. | 5 >> 1 | 2 |
Assignment Operators
Assignment operators assign values to variables and can combine assignments with operations.
Operator | Description | Example | Result |
---|---|---|---|
= | Assigns the value on the right to the variable on the left. | a = 5 | 5 |
+= | Adds the value on the right to the variable on the left, then assigns the result to the variable. | a += 3 | 8 |
-= | Subtracts the value on the right from the variable on the left, then assigns the result to the variable. | a -= 3 | 2 |
*= | Multiplies the variable by the value on the right, then assigns the result to the variable. | a *= 3 | 15 |
/= | Divides the variable by the value on the right, then assigns the result to the variable. | a /= 3 | 5 |
%= | Finds the remainder when the variable is divided by the value on the right, then assigns the result to the variable. | a %= 3 | 2 |
Ternary (Conditional) Operator
The ternary operator is a shorthand for if-else
conditions. It has the syntax:
condition ? expression1 : expression2
Example | Description | Result |
---|---|---|
5 > 3 ? "Yes" : "No" | If the condition (5 > 3 ) is true, it returns "Yes"; otherwise, it returns "No". | Yes |
Type Operators
Type operators provide information about the type of a variable or allow type assertions.
Operator | Description | Example | Result |
---|---|---|---|
typeof | Returns the type of a variable as a string. It helps in determining the type dynamically. | typeof "hello" | string |
instanceof | Checks if an object is an instance of a specific class. Returns true or false . | obj instanceof MyClass | true/false |
as | Performs a type assertion, helping TypeScript treat a variable as a specific type. | value as string | Cast to string |
By mastering these operators, you can write powerful and efficient TypeScript code.