# TypeScript Operators

An Operator is a symbol which operates on a value or data. It represents a specific action on working with data. The data on which operators operates is called operand. It can be used with one or more than one values to produce a single value. All of the standard JavaScript operators are available with the TypeScript program.

10 + 10 = 20;

In the above example, the values '10' and '20' are known as an operand, whereas '+' and '=' are known as operators.

## Operators in TypeScript

In TypeScript, an operator can be classified into the following ways.

## Arithmetic Operators

Arithmetic operators take numeric values as their operands, performs an action, and then returns a single numeric value. The most common arithmetic operators are addition(+), subtraction(-), multiplication(*), and division(/).

Operator Operator_Name Description Example
let a = 20;
let b = 30;
let c = a + b;
console.log( c ); //
Output
30
- Subtraction It returns the difference of the values.
let a = 30;
let b = 20;
let c = a - b;
console.log( c ); //
Output
10
* Multiplication It returns the product of the values.
let a = 30;
let b = 20;
let c = a * b;
console.log( c ); //
Output
600
/ Division It performs the division operation, and returns the quotient.
let a = 100;
let b = 20;
let c = a / b;
console.log( c ); //
Output
5
% Modulus It performs the division operation and returns the remainder.
let a = 95;
let b = 20;
let c = a % b;
console.log( c ); //
Output
15
++ Increment It is used to increments the value of the variable by one.
let a = 55;
a++;
console.log( a ); //
Output
56
-- Decrement It is used to decrements the value of the variable by one.
let a = 55;
a--;
console.log( a ); //
Output
54

## Comparison (Relational) Operators

The comparison operators are used to compares the two operands. These operators return a Boolean value true or false. The important comparison operators are given below.

Operator Operator_Name Description Example
== Is equal to It checks whether the values of the two operands are equal or not.
let a = 10;
let b = 20;
console.log(a==b);     //false
console.log(a==10);    //true
console.log(10=='10'); //true
=== Identical(equal and of the same type) It checks whether the type and values of the two operands are equal or not.
let a = 10;
let b = 20;
console.log(a===b);    //false
console.log(a===10);   //true
console.log(10==='10'); //false
!= Not equal to It checks whether the values of the two operands are equal or not.
let a = 10;
let b = 20;
console.log(a!=b);     //true
console.log(a!=10);    //false
console.log(10!='10'); //false
!== Not identical It checks whether the type and values of the two operands are equal or not.
let a = 10;
let b = 20;
console.log(a!==b);     //true
console.log(a!==10);    /false
console.log(10!=='10'); //true
> Greater than It checks whether the value of the left operands is greater than the value of the right operand or not.
let a = 30;
let b = 20;
console.log(a>b);     //true
console.log(a>30);    //false
console.log(20> 20'); //false
>= Greater than or equal to It checks whether the value of the left operands is greater than or equal to the value of the right operand or not.
let a = 20;
let b = 20;
console.log(a>=b);     //true
console.log(a>=30);    //false
console.log(20>='20'); //true
< Less than It checks whether the value of the left operands is less than the value of the right operand or not.
let a = 10;
let b = 20;
console.log(a<b);      //true
console.log(a<10);     //false
console.log(10<'10');  //false
<= Less than or equal to It checks whether the value of the left operands is less than or equal to the value of the right operand or not.
let a = 10;
let b = 20;
console.log(a<=b);     //true
console.log(a<=10);    //true
console.log(10<='10'); //true

## Logical Operators

Logical operators are used for combining two or more condition into a single expression and return the Boolean result true or false. The Logical operators are given below.

Operator Operator_Name Description Example
&& Logical AND It returns true if both the operands(expression) are true, otherwise returns false.
let a = false;
let b = true;
console.log(a&&b);      /false
console.log(b&&true);   //true
console.log(b&&10);     //10 which is also 'true'
console.log(a&&'10');  //false
|| Logical OR It returns true if any of the operands(expression) are true, otherwise returns false.
let a = false;
let b = true;
console.log(a||b);      //true
console.log(b||true);   //true
console.log(b||10);     //true
console.log(a||'10');   //'10' which is also 'true'
! Logical NOT It returns the inverse result of an operand(expression).
let a = 20;
let b = 30;
console.log(!true);    //false
console.log(!false);   //true
console.log(!a);       //false
console.log(!b);       /false
console.log(!null);    //true

## Bitwise Operators

The bitwise operators perform the bitwise operations on operands. The bitwise operators are as follows.

Operator Operator_Name Description Example
& Bitwise AND It returns the result of a Boolean AND operation on each bit of its integer arguments.
let a = 2;
let b = 3;
let c = a & b;
console.log(c);   //
Output 2
| Bitwise OR It returns the result of a Boolean OR operation on each bit of its integer arguments.
let a = 2;
let b = 3;
let c = a | b;
console.log(c);   //
Output 3
^ Bitwise XOR It returns the result of a Boolean Exclusive OR operation on each bit of its integer arguments.
let a = 2;
let b = 3;
let c = a ^ b;
console.log(c);   //
Output
1
~ Bitwise NOT It inverts each bit in the operands.
let a = 2;
let c = ~ a;
console.log(c);   //
Output
-3
>> Bitwise Right Shift The left operand's value is moved to the right by the number of bits specified in the right operand.
let a = 2;
let b = 3;
let c = a >> b;
console.log(c);   //
Output
0
<< Bitwise Left Shift The left operand's value is moved to the left by the number of bits specified in the right operand. New bits are filled with zeroes on the right side.
let a = 2;
let b = 3;
let c = a << b;
console.log(c);   //
Output
16
>>> Bitwise Right Shift with Zero The left operand's value is moved to the right by the number of bits specified in the right operand and zeroes are added on the left side.
let a = 3;
let b = 4;
let c = a >>> b;
console.log(c);   //
Output
0

## Assignment Operators

Assignment operators are used to assign a value to the variable. The left side of the assignment operator is called a variable, and the right side of the assignment operator is called a value. The data-type of the variable and value must be the same otherwise the compiler will throw an error. The assignment operators are as follows.

Operator Operator_Name Description Example
= Assign It assigns values from right side to left side operand.
let a = 10;
let b = 5;
console.log("a=b:" +a);   //
Output
10
+= Add and assign It adds the left operand with the right operand and assigns the result to the left side operand.
let a = 10;
let b = 5;
let c = a += b;
console.log(c);   //
Output
15
-= Subtract and assign It subtracts the right operand from the left operand and assigns the result to the left side operand.
let a = 10;
let b = 5;
let c = a -= b;
console.log(c);   //
Output
5
*= Multiply and assign It multiplies the left operand with the right operand and assigns the result to the left side operand.
let a = 10;
let b = 5;
let c = a *= b;
console.log(c);   //
Output
50
/= Divide and assign It divides the left operand with the right operand and assigns the result to the left side operand.
let a = 10;
let b = 5;
let c = a /= b;
console.log(c);   //
Output
2
%= Modulus and assign It divides the left operand with the right operand and assigns the result to the left side operand.
let a = 16;
let b = 5;
let c = a %= b;
console.log(c);   //
Output
1

## Ternary/Conditional Operator

The conditional operator takes three operands and returns a Boolean value based on the condition, whether it is true or false. Its working is similar to an if-else statement. The conditional operator has right-to-left associativity. The syntax of a conditional operator is given below.

Syntax
expression ? expression-1 : expression-2;
• expression: It refers to the conditional expression.
• expression-1: If the condition is true, expression-1 will be returned.
• expression-2: If the condition is false, expression-2 will be returned.
Example
snippet
let num = 16;
let result = (num > 0) ? "True":"False"
console.log(result);
Output
True

## Concatenation Operator

The concatenation (+) operator is an operator which is used to append the two string. In concatenation operation, we cannot add a space between the strings. We can concatenate multiple strings in a single statement. The following example helps us to understand the concatenation operator in TypeScript.

Example
snippet
let message = "Welcome to " + "rookienerd";
console.log("Result of String Operator: " +message);
Output
Result of String Operator: Welcome to rookienerd

## Type Operators

There are a collection of operators available which can assist you when working with objects in TypeScript. Operators such as typeof, instanceof, in, and delete are the examples of Type operator. The detail explanation of these operators is given below.

Operator_Name Description Example
in It is used to check for the existence of a property on an object.
let Bike = {make: 'Honda', model: 'CLIQ', year: 2018};
console.log('make' in Bike);   //
Output:
true
delete It is used to delete the properties from the objects.
let Bike = { Company1: 'Honda',
Company2: 'Hero',
Company3: 'Royal Enfield'
};
delete Bike.Company1;
console.log(Bike);   //
Output:
{ Company2: 'Hero', Company3: 'Royal Enfield' }
typeof It returns the data type of the operand.
let message = "Welcome to " + "rookienerd";
console.log(typeof message);  //
Output:
String
instanceof It is used to check if the object is of a specified type or not.
let arr = [1, 2, 3];
console.log( arr instanceof Array ); // true
console.log( arr instanceof String ); // false
Related Tutorial