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
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
8 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.Prim
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.Conditiona
4 min read
TypeScript Functions Type TypeScript function types define the structure of a function, including its parameter types and return type, ensuring consistent and type-safe usage.Help validate the types of parameters passed to a function.Ensure the function returns the expected type.Improve code clarity and prevent runtime error
6 min read
JavaScript typeof Operator The typeof operator in JavaScript is used to determine the data type of a value or variable. It returns a string indicating the type, such as "string", "number", "boolean", "object", etc.JavaScriptconsole.log(typeof "Hello"); console.log(typeof 42); console.log(typeof true); console.log(typeof {});
3 min read
TypeScript Union The TypeScript union has the ability to combine one or two different types of data (i.e., number, string, float, double, etc). It is the most powerful way to express a variable with multiple types. Use pipe ('|') symbol to combine two or more data types to achieve Union type. Syntax: (type1|type2|ty
3 min read
ES6 Operators An expression is a special kind of statement that evaluates to a value. Every expression consists of Operands: Represents the data.Operator: which performs certain operations on operands. Consider the following expression - 2 / 3, in the expression, 2 and 3 are operands and the symbol /is the operat
7 min read
JavaScript String Operators JavaScript String Operators are used to manipulate and perform operations on strings. There are two operators which are used to modify strings in JavaScript. These operators help us to join one string to another string.1. Concatenate OperatorConcatenate Operator in JavaScript combines strings using
3 min read
TypeScript Less Common Primitives Type TypeScript Less Common Primitives Type offers a rich set of primitive types to represent data. While most developers are familiar with types like number, string, boolean, and symbol, TypeScript also provides less common primitive types that can be incredibly useful in specific scenarios. These are s
2 min read
TypeScript Function Type Expressions In this article, we are going to learn about TypeScript Function Type Expressions in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, a function type expression represents the type of a function, including its parameter types
3 min read