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; |
---|
Similar Reads
TypeScript Object
A TypeScript object is a collection of key-value pairs, where keys are strings and values can be any data type. Objects in TypeScript can store various types, including primitives, arrays, and functions, providing a structured way to organize and manipulate data. What Are TypeScript Objects?An objec
5 min read
Typescript Keyof Type Operator
The TypeScript keyof operator is used to get a union of all keys in an object type. Itâs useful when you want to work with the property names of an object in a type-safe way, ensuring only valid keys are used. We can use it to define generic functions that work with any object type, without knowing
3 min read
PHP Operators
In PHP, operators are special symbols used to perform operations on variables and values. Operators help you perform a variety of tasks, such as mathematical calculations, string manipulations, logical comparisons, and more. Understanding operators is essential for writing effective and efficient PH
9 min read
TypeScript in operator narrowing Type
In this article, we will learn about the 'in' operator narrowing Type in Typescript. In TypeScript, the 'in' operator is used to narrow or refine the type of an object within a conditional statement or block. It checks whether a specific property or key exists within an object, and if it does, it na
3 min read
TypeScript Call Signatures
TypeScript call signatures define the parameter types and return types for function-like objects, enabling the creation of callable entities with additional properties. Allow objects to be invoked as functions while possessing properties.Enhance code flexibility by combining callable behavior with s
3 min read
Data types in TypeScript
In TypeScript, a data type defines the kind of values a variable can hold, ensuring type safety and enhancing code clarity. Primitive Types: Basic types like number, string, boolean, null, undefined, and symbol.Object Types: Complex structures including arrays, classes, interfaces, and functions.Pri
3 min read
TypeScript Utility Types
TypeScript utility types are predefined constructs that facilitate common type transformations, enhancing code flexibility and maintainability. They allow developers to modify existing types easily, such as by making properties optional or read-only.Utility types help in creating more expressive and
5 min read
TypeScript Programming Example
TypeScript is a popular superset of JavaScript that adds static typing and other features to improve the development experience. In this blog post, weâll explore practical examples of TypeScript that demonstrate how it can be used to solve common development challenges. Tip: Before, directly jumping
12 min read
TypeScript Assertion functions
TypeScript Assertion functions are nothing but the functions that allow us to mainly create the custom type guards that assert the type of a value on our specific criteria. The most suitable use of Assertion Functions is when we are working with the more complex data structures. Syntax:function asse
3 min read
TypeScript Conditional Types
In TypeScript, conditional types enable developers to create types that depend on a condition, allowing for more dynamic and flexible type definitions. They follow the syntax T extends U ? X : Y, meaning if type T is assignable to type U, the type resolves to X; otherwise, it resolves to Y.Condition
4 min read