The operators are used for some operation like addition or substruction, TypeScript also have defined some operators like other programming languages,
Here the addition or substruction symbol is the operator and the values to add or substruct are the operands.
Let’s discuss the operators in details:
Arithmetic Operator
There are 7 types of the arithmetic operator which are used on mathematical calculation purpose and they are as described below.
- [+]: is an addition operator
- [-]: is a subtraction operator
- [*]: is a multiplication operator
- [/]: is a division operator
- [%]: is a modulus operator
- [++] : is a increment operator
- [–]: is a decrement operator
Example
var a:number = 4;
var b:number = 8;
var c:number = a+b;
var d:number = a-b;
var e:number = a*b;
var f:number = a/b;
var g:number = a%b;
a++;
b--;
Relational operator:
There is 6 type of relational operator also which are used to check conditional logic. and they are
- [>] is greater than the operator
- [<] is a lesser Than operator
- [>=] is greater than equals operator
- [<=] is lesser than equals operator
- [==] is an equals operator
- [!=] is a not equals operator
Example:
var m:number = 4;
var n:number = 8;
if(m>n){ console.log('m>n'); }
if(m=n){ console.log('m>=n'); }
if(m<=n){ console.log('m<=n'); }
if(m==n){ console.log('m==n'); }
if(m!=n){ console.log('m!=n'); }
Logical Operator:
There are 3 types of Logical Operator they are also used on conditional logic and they are
- [&&] is a logical and operator
- [||] is a Logical or operator
- [!] is a logical not operator
Example:
var o:number = 4;
var p:number = 8;
var q:number = 8;
var r:number;
if(o>p && q>p){ console.log('o>p && q>p'); }
if(o>p || q>p){ console.log('o>p && q>p'); }
if(!r){ console.log('r have no value'); }
Bitwise Operator:
There is 7 Bitwise Operator and they are as described below.
- [&] is a Bitwise AND operator
- [|] is a BitWise OR operator
- [^] is a Bitwise XOR operator
- [~] is a Bitwise Not operator
- [<<] is a Left Shift operator
- [>>] is a Right Shift operator
- [>>>] is a Right shift with Zero operator
Example:
var t:number = 8;
var u = s&t;
var v = s|t;
var w = s^t;
var X = s~t;
var y = s<>t;
var Zz = s>>>t;
Assignment operators
There are 5 assignment operators which are used to assign value in a variable or array and they are as described below,
- [=] is a simple assignment operator
- [+=] is an add and assignment operator
- [-=] is a subtract and assignment operator
- [*=] is a multiply and assignment operator
- [/=] is a divide and assignment operator
Example:
var i = 10;
var i += 10;
var i -= 5;
var i *= 2;
var i /= 3;
Miscellaneous Operators
There are some miscellaneous Operators also in typescript and they are as described below
- [-]: The negation operator: if there a – placed before a variable it will make the value of the variable negative
- [+]: Concatenation Operator: a + sign between two strings made the string concatenate
- [?]: Conditional Operator: There is a conditional operator called ? is mainly used for if case
- [typeof]: Type Operator: There is another operator called type operator, which described the type of the value of a variable
Example:
var x = 4, var y = -x;
var str1 = 'some string'; var str2 = 'more string'; var str_conc = str1 + str2;
1=1 ? console.log('Condition is true') : console.log('Condition is false') ;
var a_num = 12; console.log(typeof a_num); // Note: Here the the putput will be number