TypeScript Operators

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.

  1. [+]: is an addition operator
  2. [-]: is a subtraction operator
  3. [*]: is a multiplication operator
  4. [/]: is a division operator
  5. [%]: is a modulus operator
  6. [++] : is a increment operator
  7. [–]: 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

  1. [>] is greater than the operator
  2. [<] is a lesser Than operator
  3. [>=] is greater than equals operator
  4. [<=] is lesser than equals operator
  5. [==] is an equals operator
  6. [!=] 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

  1. [&&] is a logical and operator
  2. [||] is a Logical or operator
  3. [!] 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.

  1. [&] is a Bitwise AND operator
  2. [|] is a BitWise OR operator
  3. [^] is a Bitwise XOR operator
  4. [~] is a Bitwise Not operator
  5. [<<] is a Left Shift operator
  6. [>>] is a Right Shift operator
  7. [>>>] 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,

  1. [=] is a simple assignment operator
  2. [+=] is an add and assignment operator
  3. [-=] is a subtract and assignment operator
  4. [*=] is a multiply and assignment operator
  5. [/=] 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

  1. [-]: The negation operator: if there a – placed before a variable it will make the value of the variable negative
  2. [+]: Concatenation Operator: a + sign between two strings made the string concatenate
  3. [?]: Conditional Operator: There is a conditional operator called ? is mainly used for if case
  4. [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

Share The Post On -

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.