TypeScript Operators
Last Updated :
13 Jan, 2024
TypeScript operators are symbols or keywords that perform operations on one or more operands. In this article, we are going to learn various types of TypeScript Operators.
Below are the different TypeScript Operators:
TypeScript Arithmetic operators
In TypeScript, arithmetic operators are used to perform mathematical calculations.
Adds two values or expressions. |
a + b |
Subtracts the right operand from the left operand. |
a - b |
Multiplies two values or expressions |
a * b |
Divides the left operand by the right operand. |
a / b |
Returns the remainder of the division of the left operand by the right operand. |
a % b |
Increase the value of the operand by 1. |
a++ or ++a |
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.
Returns true if both operands are true .
|
result = operand1 && operand2;
|
Returns true if at least one of the operands is true .
|
result = operand1 || operand2;
|
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.
Returns true if the values of the two operands are equal, after type coercion.
|
result = operand1 == operand2;
|
Returns true if the values of the two operands are not equal, after type coercion.
|
result = operand1 != operand2;
|
Returns true if the values of the two operands are equal, without type coercion (strict equality).
|
result = operand1 === operand2;
|
Returns true if the values of the two operands are not equal, without type coercion (strict inequality).
|
result = operand1 !== operand2;
|
Returns true if the value of the left operand is greater than the value of the right operand.
|
result = operand1 > operand2;
|
Returns true if the value of the left operand is less than the value of the right operand.
|
result = operand1 < operand2;
|
Returns true if the value of the left operand is greater than or equal to the value of the right operand.
|
result = operand1 >= operand2;
|
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.
Performs a bitwise AND operation between each pair of corresponding bits.
|
result = operand1 & operand2;
|
Performs a bitwise OR operation between each pair of corresponding bits.
|
result = operand1 | operand2;
|
Performs a bitwise XOR (exclusive OR) operation between each pair of corresponding bits.
|
result = operand1 ^ operand2;
|
Inverts the bits of the operand, changing each 0 to 1 and each 1 to 0 .
|
result = ~operand;
|
Shifts the bits of the left operand to the left by the number of positions specified by the right operand.
|
result = operand1 << operand2;
|
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;
|
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.
Assigns the value of the right operand to the left operand. |
variable = value; |
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; |
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; |
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; |
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; |
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.
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.
Obtains the type of a variable, function, or property. |
let x = 10; <br>type XType = typeof x; <br>// XType is 'number' |
Obtains the keys (property names) of a type. |
type Person = { name: string; age: number }; <br>type PersonKeys = keyof Person; <br>`// PersonKeys is ‘name’ |
Allows creating new types based on the properties of existing types. |
type Optional<T> = { [K in keyof T]?: T[K] }; |
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.
Concatenates two strings. |
let fullName = firstName + " " + lastName; |
Allows embedding expressions inside strings. |
let message = \ I am ${age} years old.`;` |
Similar to template literals, it allows inserting variables into strings. |
let description = "I live in " + city + "."; |
Various methods for manipulating strings. |
let substring = phrase.substring(7, 15); |
Returns the length of a string. |
let length = message.length; |
Share your thoughts in the comments
Please Login to comment...