TypeScript operators are symbols or keywords that perform operations on one or more operands.
Below are the different TypeScript Operators:
TypeScript Arithmetic operators
In TypeScript, arithmetic operators are used to perform mathematical calculations.
Name | Description | Syntax |
|---|
Addition(+) | Adds two values or expressions. | a + b |
|---|
Subtraction(-) | Subtracts the right operand from the left operand. | a - b |
|---|
Multiplication(*) | Multiplies two values or expressions | a * b |
|---|
Division(/) | Divides the left operand by the right operand. | a / b |
|---|
Modulus(%) | Returns the remainder of the division of the left operand by the right operand. | a % b |
|---|
Increment(++) | Increase the value of the operand by 1. | a++ or ++a |
|---|
Decrement(--) | Decrease the value of the operand by 1. | a-- or --a |
|---|
TypeScript Logical operators
In TypeScript, logical operators are used to perform logical operations on Boolean values.
Name | Description | Syntax |
|---|
Logical AND (&&) | Returns true if both operands are true. | result = operand1 && operand2; |
|---|
Logical OR (||) | Returns true if at least one of the operands is true. | result = operand1 || operand2; |
|---|
Logical NOT (!) | Returns true if the operand is false, and vice versa. | result = !operand; |
|---|
TypeScript Relational operators
In TypeScript, relational operators are used to compare two values and determine the relationship between them.
Name | Description | Syntax |
|---|
Equal to (==) | Returns true if the values of the two operands are equal, after type coercion. | result = operand1 == operand2; |
|---|
Not equal to (!=) | Returns true if the values of the two operands are not equal, after type coercion. | result = operand1 != operand2;
|
|---|
Strictly equal to (===) | Returns true if the values of the two operands are equal, without type coercion (strict equality). | result = operand1 === operand2; |
|---|
Strictly not equal to (!==) | Returns true if the values of the two operands are not equal, without type coercion (strict inequality). | result = operand1 !== operand2; |
|---|
Greater than (>) | Returns true if the value of the left operand is greater than the value of the right operand. | result = operand1 > operand2; |
|---|
Less than (<) | Returns true if the value of the left operand is less than the value of the right operand. | result = operand1 < operand2; |
|---|
Greater than or equal to (>=) | Returns true if the value of the left operand is greater than or equal to the value of the right operand. | result = operand1 >= operand2; |
|---|
Less than or equal to (<=) | Returns true if the value of the left operand is less than or equal to the value of the right operand | result = operand1 <= operand2; |
|---|
TypeScript Bitwise operators
In TypeScript, bitwise operators perform operations on the binary representation of numeric values.
Name | Description | Syntax |
|---|
Bitwise AND (&) | Performs a bitwise AND operation between each pair of corresponding bits. | result = operand1 & operand2; |
|---|
Bitwise OR (|) | Performs a bitwise OR operation between each pair of corresponding bits. | result = operand1 | operand2; |
|---|
Bitwise XOR (^) | Performs a bitwise XOR (exclusive OR) operation between each pair of corresponding bits. | result = operand1 ^ operand2; |
|---|
Bitwise NOT (~) | Inverts the bits of the operand, changing each 0 to 1 and each 1 to 0. | result = ~operand; |
|---|
Left Shift (<<) | Shifts the bits of the left operand to the left by the number of positions specified by the right operand. | result = operand1 << operand2; |
|---|
Sign-propagating Right Shift (>>) | Shifts the bits of the left operand to the right by the number of positions specified by the right operand, preserving the sign bit. | result = operand1 >> operand2; |
|---|
Zero-fill Right Shift (>>>) | Shifts the bits of the left operand to the right by the number of positions specified by the right operand, filling the leftmost bits with zeros. | result = operand1 >>> operand2; |
|---|
TypeScript Assignment operators
In TypeScript, assignment operators are used to assign values to variables and modify their values based on arithmetic or bitwise operations.
| Name | Description | Syntax |
|---|
Assignment (=) | Assigns the value of the right operand to the left operand. | variable = value; |
|---|
Addition Assignment (+=) | Adds the value of the right operand to the current value of the left operand and assigns the result to the left operand. | variable += value; |
|---|
Subtraction Assignment (-=) | Subtracts the value of the right operand from the current value of the left operand and assigns the result to the left operand. | variable -= value; |
|---|
Multiplication Assignment (*=) | Multiplies the current value of the left operand by the value of the right operand and assigns the result to the left operand. | variable *= value; |
|---|
Division Assignment (/=) | Divides the current value of the left operand by the value of the right operand and assigns the result to the left operand. | variable /= value; |
|---|
Modulus Assignment (%=) | Calculates the remainder when dividing the current value of the left operand by the value of the right operand and assigns the result to the left operand. | variable %= value; |
|---|
TypeScript Ternary/conditional operator
In TypeScript, the ternary operator, also known as the conditional operator, is a concise way to write conditional statements. It allows you to express a simple if-else statement in a single line.
| Name | Description | Syntax |
|---|
| Ternary/Conditional Operator | Evaluates the condition. If true, returns expression_if_true; if false, returns expression_if_false. | condition ? expression_if_true : expression_if_false; |
|---|
TypeScript Type Operators
In TypeScript, type operators are constructs that allow you to perform operations on types. These operators provide powerful mechanisms for defining and manipulating types in a flexible and expressive manner.
| Name | Description | Syntax |
|---|
| typeof | Obtains the type of a variable, function, or property. | let x = 10;<br>type XType = typeof x;<br>// XType is 'number' |
|---|
| keyof | Obtains the keys (property names) of a type. | type Person = { name: string; age: number };<br>type PersonKeys = keyof Person;<br>`// PersonKeys is 'name' |
|---|
| Mapped Types | Allows creating new types based on the properties of existing types. | type Optional<T> = { [K in keyof T]?: T[K] }; |
|---|
| Conditional Types | Allows expressing a type based on a condition. | type TypeName<T> = T extends string ? 'string' : 'non-string'; |
|---|
TypeScript String Operators
In TypeScript, string operators and features are used for manipulating and working with string values.
| Name | Description | Syntax |
|---|
String Concatenation (+) | Concatenates two strings. | let fullName = firstName + " " + lastName; |
|---|
Template Literals (`) | Allows embedding expressions inside strings. | let message = I am ${age} years old.`;` |
|---|
| String Interpolation | Similar to template literals, it allows inserting variables into strings. | let description = "I live in " + city + "."; |
|---|
| String Methods | Various methods for manipulating strings. | let substring = phrase.substring(7, 15); |
|---|
String Length Property (length) | Returns the length of a string. | let length = message.length; |
|---|
Explore
TypeScript Tutorial
8 min read
TypeScript Basics
TypeScript primitive types
TypeScript Object types
TypeScript other types
TypeScript combining types
TypeScript Assertions
TypeScript Functions
TypeScript interfaces and aliases
TypeScript classes