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:
Operator | Description | Example |
+ | Addition | 30+20 = 50 |
– | Subtraction | 30-10 = 20 |
* | Multiplication | 10*10 = 100 |
/ | Division | 50/10 = 5 |
Modulus (Remainder) | 50%7 = 1 | |
++ | Increment | var a=10; a++; Result into a = 11 |
— | Decrement | var 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:
Operator | Description | Example |
== | Is equal to | 10==30 = false |
=== | Identical (equal and of same type) | 10==40 = false |
!= | Not equal to | 20!=20 = true |
!== | Not Identical | 20!==20 = false |
> | Greater than | 30>10 = true |
>= | Greater than or equal to | 40>=10 = true |
< | Less than | 25<10 = false |
<= | Less than or equal to | 25<=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 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:
Operator | Description | Example |
& | 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:
Operator | Description | Example |
&& | 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:
Operator | Description | Example |
= | Assign | a = 10 + 10; Result into a = 20 |
+= | Add and assign | var a=10; a+=20; Result into a = 30 |
-= | Subtract and assign | var a=20; a+=10; Result into a = 10 |
*= | Multiply and assign | var a=10; a*=20; Result into a = 200 |
/= | Divide and assign | var a=10; a/=2; Result into a = 5 |
Modulus and assign | var a=10; |
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 console.log("
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);