TypeScript Arithmetic and Relational Operators
- Admin
- Mar 6, 2024
- Typescript
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.
Operator | Title | Description | Example |
---|---|---|---|
+ | Addition | Computes the sum of two or more values | p+q=50 |
- | Subtraction | calculates subtraction of two or more values | q-p=10 |
* | Multiplication | Computes multiplication of two or more values | p*q=600 |
/ | Divide | Calculate the division of values | q/p=1.5 |
% | Modulus | Returns the remainder after division | q%p=10 |
++ | Increment | Increment the value by 1 | ++p=21 |
-- | Decrement | Decrement 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.
Operator | Title | Example |
---|---|---|
< | Less than | M<N is false |
> | Greater than | M>N returns false |
<= | Less than equal | M<=N returns false |
>= | Greater than equal | M>=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.
Operator | Title | Description | Example |
---|---|---|---|
== | Equality | Returns boolean after comparing values; applies strict comparison if types are not equal | M==N returns false |
!= | Not equal | Returns boolean following similar logic to equality operator but with inequality check | M!=N returns true |
=== | Identity Operator | Checks strict equality without type conversion | M!=N returns true |
!== | Non Identity | Checks non-strict equality without type consideration | M!=N returns true |
Conclusion
In summary, you’ve learned about TypeScript operators with examples.
- Arithmetic operator
- Relational operator
- Equality operator