TypeScript operators list

Commonly used TypeScript operators list:

  • Arithmetic Operators
  • Comparison Operators
  • Bitwise Operators
  • Logical Operators
  • Assignment Operators
  • Conditional Operator
  • String Operator
  • typeof Operator

 

TypeScript Arithmetic Operators:

Arithmetic operators are used to perform arithmetic operations.

TypeScript Arithmetic Operators List:

OperatorDescriptionExample
+Addition30+20 = 50
Subtraction30-10 = 20
*Multiplication10*10 = 100
/Division50/10 = 5
%Modulus (Remainder)50%7 = 1
++Incrementvar a=10; a++; Result into a = 11
Decrementvar a=10; a–; Result into a = 9

TypeScript Arithmetic Operators Example:

var num1:number = 20;
var num2:number = 4;
var result:number = 0;
 
result = num1 + num2;
console.log("Sum: "+result); 
 
result = num1 - num2; 
console.log("Difference: "+result);
 
result = num1*num2;
console.log("Product: "+result);
 
result = num1/num2;
console.log("Quotient: "+result);
 
result = num1%num2;
console.log("Remainder: "+result);
 
num1++ 
console.log("Value of num1 after increment: "+num1);
 
num2-- 
console.log("Value of num2 after decrement: "+num2);

TypeScript Comparison Operators:

TypeScript comparison operators are used to compare the two operands.

TypeScript Comparison Operators List:

OperatorDescriptionExample
==Is equal to10==30 = false
===Identical (equal and of same type)10==40 = false
!=Not equal to20!=20 = true
!==Not Identical20!==20 = false
>Greater than30>10 = true
>=Greater than or equal to40>=10 = true
<Less than25<10 = false
<=Less than or equal to25<=10 = false

TypeScript Comparison Operators Example:

var num1:number = 15;
var num2:number = 17;
 
console.log("Value of num1: "+num1); 
console.log("Value of num2 :"+num2);
 
var result = num1>num2 
console.log("num1 greater than num2: "+result)
 
result = num1<num2 
console.log("num1 lesser than num2: "+result)  
 
result = num1>=num2 
console.log("num1 greater than or equal to  num2: "+result)
 
result = num1<=num2
console.log("num1 lesser than or equal to num2: "+result)  
 
result = num1==num2 
console.log("num1 is equal to num2: "+result)  
 
result = num1!=num2  
console.log("num1 is not equal to num2: "+result)

TypeScript Bitwise Operators:

JavaScript bitwise operators are used to perform bitwise operations on the operands

TypeScript Bitwise Operators List:

OperatorDescriptionExample
&Bitwise AND(10==25 & 20==35) = false
|Bitwise OR(20==30 | 20==40) = false
^Bitwise XOR(20==30 ^ 30==40) = false
~Bitwise NOT(~20) = -20
<<Bitwise Left Shift(10<<2) = 40
>>Bitwise Right Shift(10>>2) = 2
>>>Bitwise Right Shift with Zero(10>>>2) = 2

TypeScript Bitwise Operators Example:

var a:number = 2;   
var b:number = 3;
 
var result; 
 
result = (a & b);     
console.log("(a & b) => ",result);
 
result = (a | b);          
console.log("(a | b) => ",result);  
 
result = (a ^ b);  
console.log("(a ^ b) => ",result);
 
result = (~b); 
console.log("(~b) => ",result);
 
result = (a << b); 
console.log("(a << b) => ",result); 
 
result = (a >> b); 
console.log("(a >> b) => ",result);

Typescript Logical Operators:

Typescript logical operators are used to perform logical operations on the operands.

Typescript Logical Operators List:

OperatorDescriptionExample
&&Logical AND(20==40 && 20==30) = false
||Logical OR(20==40 || 20==30) = false
!Logical Not!(20==30) = true

TypeScript Logical Operators Example:

var avg:number = 20; 
var percentage:number = 90; 
 
console.log("Value of avg: "+avg+" ,value of percentage: "+percentage);
 
var result:boolean = ((avg>50)&&(percentage>80)); 
console.log("(avg>50)&&(percentage>80): ",result);
 
var result:boolean = ((avg>50)||(percentage>80)); 
console.log("(avg>50)||(percentage>80): ",result);
 
var result:boolean=!((avg>50)&&(percentage>80)); 
console.log("!((avg>50)&&(percentage>80)): ",result);

TypeScript Assignment Operators:

TypeScript assignment operators are used to assign the values to the operands.

TypeScript Assignment Operators List:

OperatorDescriptionExample
=Assigna = 10 + 10; Result into a = 20
+=Add and assignvar a=10; a+=20; Result into a = 30
-=Subtract and assignvar a=20; a+=10; Result into a = 10
*=Multiply and assignvar a=10; a*=20; Result into a = 200
/=Divide and assignvar a=10; a/=2; Result into a = 5
%=Modulus and assignvar a=10; a%=2; Result into a = 0

TypeScript Assignment Operators Example:

var a:number = 20 
var b:number = 15  
 
a = b 
console.log("a = b: "+a)
 
a += b
console.log("a+=b: "+a)
 
a -= b 
console.log("a-=b: "+a)
 
a *= b 
console.log("a*=b: "+a)
 
a /= b 
console.log("a/=b: "+a)    
 
a %= b 
console.log("a%=b: "+a)

Typescript Conditional Operator:

Typescript conditional operator is used as a shortcut for the if statement. It takes three operands. If condition is true, the operator returns the value of expr1 otherwise it returns the value of expr2.

Typescript Conditional Operator Syntax:

condition ? expr1 : expr2

TypeScript Conditional Operator Example:

var num:number = 10; 
var result = num > 0 ?"positive":"negative" ;
console.log(result);

Typescript String Operator:

Typescript string operator refers to (+) which when used with two strings, it appends the second string to the first.

TypeScript String Operator Example:

var msg:string = "Hello "+"jai" 
console.log(msg)

Typescript typeof Operator:

Typescript typeof operator returns the type of object

TypeScript typeof Operator Example:

var name = "Jai";
console.log(typeof name);
Content Protection by DMCA.com
Please Share