Programming Language
TypeScript
Operators
TypeScript Operators

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.

OperatorDescriptionExampleResult
+Adds two numbers. The result is their sum.5 + 38
-Subtracts the second number from the first. The result is their difference.5 - 32
*Multiplies two numbers. The result is their product.5 * 315
/Divides the first number by the second. The result is their quotient.6 / 32
%Finds the remainder when the first number is divided by the second.5 % 32
++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.

OperatorDescriptionExampleResult
&&Returns true if both conditions are true. Otherwise, it returns false.true && falsefalse
││Returns true if at least one condition is true. Otherwise, it returns false.true ││ falsetrue
!Reverses the value of a condition. If it's true, it becomes false, and vice versa.!truefalse

Relational Operators

Relational operators compare two values and return a boolean (true or false).

OperatorDescriptionExampleResult
==Checks if two values are equal. It doesn't consider data types.5 == 5true
!=Checks if two values are not equal. It doesn't consider data types.5 != 3true
>Checks if the first value is greater than the second.5 > 3true
<Checks if the first value is less than the second.5 < 3false
>=Checks if the first value is greater than or equal to the second.5 >= 5true
<=Checks if the first value is less than or equal to the second.5 <= 3false

Bitwise Operators

Bitwise operators perform operations at the binary level.

OperatorDescriptionExampleResult
&Performs a bitwise AND operation. Sets a bit to 1 if both corresponding bits are 1.5 & 31
Performs a bitwise OR operation. Sets a bit to 1 if at least one corresponding bit is 1.5 │ 37
^Performs a bitwise XOR operation. Sets a bit to 1 if only one of the corresponding bits is 1.5 ^ 36
~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 << 110
>>Shifts the bits of the first operand to the right by the specified number of places.5 >> 12

Assignment Operators

Assignment operators assign values to variables and can combine assignments with operations.

OperatorDescriptionExampleResult
=Assigns the value on the right to the variable on the left.a = 55
+=Adds the value on the right to the variable on the left, then assigns the result to the variable.a += 38
-=Subtracts the value on the right from the variable on the left, then assigns the result to the variable.a -= 32
*=Multiplies the variable by the value on the right, then assigns the result to the variable.a *= 315
/=Divides the variable by the value on the right, then assigns the result to the variable.a /= 35
%=Finds the remainder when the variable is divided by the value on the right, then assigns the result to the variable.a %= 32

Ternary (Conditional) Operator

The ternary operator is a shorthand for if-else conditions. It has the syntax:

condition ? expression1 : expression2
ExampleDescriptionResult
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.

OperatorDescriptionExampleResult
typeofReturns the type of a variable as a string. It helps in determining the type dynamically.typeof "hello"string
instanceofChecks if an object is an instance of a specific class. Returns true or false.obj instanceof MyClasstrue/false
asPerforms a type assertion, helping TypeScript treat a variable as a specific type.value as stringCast to string

By mastering these operators, you can write powerful and efficient TypeScript code.