Arithmetic and Relational Operator in typescript

This tutorial explains Arithmetic and Relational Operator in typescript with examples.

Typescript arithmetic operator

Arithmetic operators are used in expressions with two or more entries.

This operator will work on numeric values.

These operators are very easy to understand as these are available in our study curriculums as well as other programming languages.

This evaluates Arithmetic operators on two operands.

Here is a syntax

operand1 operator operand2

Assume that p has a value of 20, and q has a value of 30.

OperatorTitleDescriptionExample
+Additionreturn values which are computed addition of two or more valuesp+q=50
-Subtractionreturn values which are computed subtraction of two or more valuesq-p=10
*Multiplicationreturn values which are computed multiplication of two or more valuesp*q=600
/DivideCalculate the division of valuesq/p=1.5
%ModulusReturn the remainder after the division of valuesq%p=10
++IncrementIncrement the value by 1++p=21
--DecrementDecrement the value by 1--q=29

arithmetic operator Example

Here is an arithmetic operator example

var m = 50;
var n = 20;
console.log(m + n); // returns 70
console.log(m - n); // returns 30
console.log(m * n); // returns 1000
console.log(m / n); // returns 2.5
console.log(m % n); // returns 10
console.log(++m); // returns 51
console.log(--n); // returns 19

Relational Operators in typescript

The Relational operator returns true or false to check operands.

Assume values of M are 10 and N is 20.

OperatorTitleExample
<Less thanM<N is false
>Greater thanM>N returns false
<=Less than equalM<=N returns false
>=Greater than equalM>=N returns false

Following is a Relational Operator Example

var m = 50;
var n = 20;
console.log(mn); // returns true
console.log(m <= n); // returns false
console.log(m >= n); // returns true

Typescript Equality Operators

These operators check the equality of operands.

assume values of C are 10 and D is 20

OperatorTitleDescriptionExample
==Equalityreturns boolean, first check if types are equal, then compare the values of types are objects, then compares references of the same objects if types are not equal, then it will apply strict comparisonM==N returns false
!=Not equalIt will also do the same logic as the above operator with not equal checkM!=N returns true
===Identity OperatorThis will checks strict equality without doing type conversionM!=N returns true
!==Non IdentityThis will check non-strict equality without checking the same typesM!=N returns true

Conclusion

To summarize, you learned about typescript operators with examples.

  • Arithmetic operator
  • Relational operator
  • Equality operator