Javatpoint Logo
Javatpoint Logo

TypeScript Operators

An Operator is a symbol that operates on a value or data. It represents a specific action on working with data. The data on which operators performs the operation 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 within our TypeScript program.

Example:

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

Operators in TypeScript:

The operators in TypeScript can be classified as-

  • Arithmetic operators
  • Comparison (Relational) operators
  • Logical operators
  • Bitwise operators
  • Assignment operators
  • Ternary/conditional operator
  • Concatenation operator
  • Type Operator

Arithmetic Operators

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

Operator Operator_Name Description Example
+ Addtion It returns the addition of the values.
let a = 20;
let b = 30;
let c = a + b;
console.log( c ); //
Output
30
- Substraction 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
% Modulous It performs 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 operator compares the two operands. These operators return a Boolean value, i.e., true/ false. The comparison operators are as follow:

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
=== Identica(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 to combining two or more condition into a single expression and return the boolean result true or false. The Logical operators are as follow:

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 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

The bitwise 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 returns value based on the condition. It is also known as a ternary operator. It is like if-else. It is evaluated as a boolean value. The conditional operator has right-to-left associativity. The Conditional operator syntax is given below.

  • 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:

Output:

True

Concatenation Operator

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

Example:

Output:

Result of String Operator: Welcome to JavaTpoint

Type Operators

There are a collection of operators available that can assist you when working with objects in TypeScript. Operators such as typeof, instanceof, in, and delete are the examples of Type operator. These operators are explained 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 " + "JavaTpoint";
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




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


B.Tech / MCA