TypeScript Arithmetic and Relational Operators

This tutorial explains TypeScript arithmetic and relational operators with examples.

TypeScript Arithmetic Operator

Arithmetic operators are used in expressions involving two or more opernads, working exclusively with numeric values. These operators are fundamental concepts present in study curriculums and various programming languages.

They operate on two operands with the following syntax:

operand1 operator operand2

Let’s assume p has a value of 20, and q has a value of 30.

OperatorTitleDescriptionExample
+AdditionComputes the sum of two or more valuesp+q=50
-Subtractioncalculates subtraction of two or more valuesq-p=10
*MultiplicationComputes multiplication of two or more valuesp*q=600
/DivideCalculate the division of valuesq/p=1.5
%ModulusReturns the remainder after divisionq%p=10
++IncrementIncrement the value by 1++p=21
--DecrementDecrement the value by 1--q=29

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(m < n);  // returns false
console.log(m <= n); // returns false
console.log(m >= n); // returns true

TypeScript Equality Operators

These operators check the equality of operands.

Assuming C has a value of 10 and D is 20.

OperatorTitleDescriptionExample
==EqualityReturns boolean after comparing values; applies strict comparison if types are not equalM==N returns false
!=Not equalReturns boolean following similar logic to equality operator but with inequality checkM!=N returns true
===Identity OperatorChecks strict equality without type conversionM!=N returns true
!==Non IdentityChecks non-strict equality without type considerationM!=N returns true

Conclusion

In summary, you’ve learned about TypeScript operators with examples.

  • Arithmetic operator
  • Relational operator
  • Equality operator