# TypeScript Operators

In typescript, operator is a programming element or symbol that specifies what kind of an operation can be performed on operands or variables. For example, an addition (+) operator in typescript is useful to perform sum operation on operands.

In typescript, we have a different type of operators available, those are:

• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Conditional Operators

## TypeScript Arithmetic Operators

In TypeScript, arithmetic operators are useful to perform the mathematical operations like addition (+), subtraction (-), multiplication (*), division (/), etc. based on our requirements.

For example, we have two variables x = 100, y = 50 and we can perform arithmetic operations on these variables like as following.

let x: number = 100;

let y: number = 50;

console.log('Substraction:', x - y);

console.log('Multiplcation:', x * y);

console.log('Division:', x / y);

When we compile and execute the above typescript program, we will get the result like as below.

Substraction: 50

Multiplcation: 5000

Division: 2

Following table lists the different type of operators available in typescript arithmetic operators.

OperatorNameDescriptionExample (a = 6, b = 3)
- Subtraction It subtract two operands. a - b = 3
* Multiplication It multiplies two operands. a * b = 18
/ Division It divides numerator by de-numerator. a / b = 2
% Modulo It returns a remainder as result. a % b = 0
++ Increment It increment the value of variable by one. a++ is 7
-- Decrement It decrement the value of variable by one. a-- is 5

## TypeScript Relational Operators

In typescript, Relational Operators are useful to check the relation between two operands like we can determine whether two operand values equal or not, etc. based on our requirements.

For example, we have two variables x = 100, y = 200 and we can perform comparison (relational) operations on these variables like as following.

let x = 100, y = 200;

if (x == y) {

console.log('Both are equal');

}

if (x != y) {

console.log('Both are not equal');

}

if (x > y) {

console.log('x is greater than y');

}

if (x < y) {

console.log('x is less than y');

}

if (x >= y) {

console.log('x is greater than or equal to y');

}

if (x <= y) {

console.log('x is less than or equal to y');

}

When we compile and execute the above typescript program, we will get the result as below.

Both are not equal

x is less than y

x is less than or equal to y

Following table lists the different type of operators available in typescript relational operators.

OperatorNameDescriptionExample (a = 6, b = 3)
== Equal to It compare two operands and return true if both are same. a == b (false)
> Greater than It compare whether left operand greater than right operand or not and return true if it satisfied. a > b (true)
< Less than It compare whether left operand less than right operand or not and return true if it satisfied. a < b (false)
>= Greater than or Equal to It compare whether left operand greater than or equal to right operand or not and return true if it satisfied. a >= b (true)
<= Less than or Equal to It compare whether left operand less than or equal to right operand or not and return true if it satisfied. a <= b (false)
!= Not Equal to It checks whether two operand values equal or not and return true if values are not equal. a != b (true)

## TypeScript Logical Operators

In typescript, Logical Operators are useful to perform the logical operation between two operands like AND, OR and NOT based on our requirements.

The Logical Operators will always work with Boolean expressions (true or false) and return Boolean values.

Following is the example of performing the logical operations on required variables in typescript.

var x: boolean = true, y: boolean = false;

var result: boolean;

// AND Operator

result = x && y;

console.log('AND Operator: ', result);

// OR Operator

result = x || y;

console.log('OR Operator: ', result);

// NOT Operator

result = !x;

console.log('NOT Operator: ', result);

When we compile and execute the above typescript program, we will get the result like as below.

AND Operator:  false

OR Operator:  true

NOT Operator:  false

Following table lists the different type of operators available in typescript logical operators.

OperatorNameDescriptionExample (a = true, b = false)
&& Logical AND It return true if both operands are non zero. a && b (false)
|| Logical OR It returns true if any one operand become a non zero. a || b (true)
! Logical NOT It return the reverse of logical state that means if both operands are non zero then it will return false. !(a && b) (true)

## TypeScript Bitwise Operators

In typescript, the Bitwise Operators will work on bits and these are useful to perform a bit by bit operations on operands.

For example, we have variables x = 10, y = 20 and the binary format of these variables will be like as shown below.

x = 10 (00001010)

y = 20 (00010100)

When we apply Bitwise OR (|) operator on these parameters we will get the result like as shown below.

00001010

00010100

-----------

00011110 = 30

Following table lists the different type of operators available in typescript bitwise operators.

OperatorNameDescriptionExample (a = 0, b = 1)
& Bitwise AND It compares each bit of first operand with the corresponding bit of its sencond operand. If both bits are 1, then the result bit will be 1 otherwise the result bit will be 0. a & b (0)
| Bitwise OR It compares each bit of first operand with the corresponding bit of its sencond operand. If either of bit is 1, then the result bit will be 1 otherwise the result bit will be 0. a | b (1)
^ Bitwise Exclusive OR (XOR) It compares each bit of first operand with the corresponding bit of its sencond operand. If one bit is 0 and other bit is 1, then the result bit will be 1 otherwise the result bit will be 0. a ^ b (1)
~ Bitwise Complement It operates on only one operand and it will invert each bit of operand. It will change bit 1 to 0 and vice versa. ~(a) (1)
<< Bitwise Left Shift) It shifts the number to the left based on the specified number of bits. The zeroes will be added to the least significant bits. b << 2 (100)
>> Bitwise Right Shift It shifts the number to the right based on the specified number of bits. The zeroes will be added to the least significant bits. b >> 2 (001)

## TypeScript Assignment Operators

In typescript, the Assignment Operators are useful to assign a new value to the operand and these operators will work with only one operand.

For example, we can declare and assign a value to the variable using assignment operator (=) like as shown below.

let x: number = 20;

x += 10;

x %= 7;

console.log("Modulo Assignment: ", x);

x *= 10;

console.log("Multiply Assignment: ", x);

x /= 10;

console.log("Division Assignment: ", x);

x -= 10;

console.log("Subtract Assignment: ", x);

When we compile and execute the above typescript program, we will get the result like as below.

Modulo Assignment:  2

Multiply Assignment:  20

Division Assignment:  2

Subtract Assignment:  -8

Following table lists the different type of operators available in typescript assignment operators.

OperatorNameDescriptionExample
= Equal to It is used to assign the values to variables. int a; a = 10
+= Addition Assignment It perform an addition of left and right operands and assign a result to the left operand. a += 10 is equals to a = a + 10
-= Subtraction Assignment It perform a subtraction of left and right operands and assign a result to the left operand. a -= 10 is equals to a = a - 10
*= Multiplication Assignment It perform a multiplication of left and right operands and assign a result to the left operand. a *= 10 is equals to a = a * 10
/= Division Assignment It perform a divison of left and right operands and assign a result to the left operand. a /= 10 is equals to a = a / 10
%= Modulo Assignment It perform a modulo operation on two operands and assign a result to the left operand. a %= 10 is equals to a = a % 10

## TypeScript Miscellaneous Operators

Same like, arithmetic, comparison, etc. we have other miscellaneous operators available in typescript, those are:

• Concatenation operator (+)
• Conditional Operator (?)
• Type Operators

We will learn about these operators in next typescript chapters.