Introduction
In a calculator, we operate with two or more numbers by adding, subtracting, multiplying, or dividing the operands (numbers). The goal is to operate on the numbers to obtain a specific result. It is crucial to understand the fundamental operators provided to perform operations in programming.
An operator is a symbol that is responsible for performing operations.
As an example, let us consider the expression,
5+20=25
In this case, the numbers 5 and 20 are operands, and the symbols + and = are called operators. The + operator is one of the many operators available in Typescript.
Let us dive deeper into the types of operators:
- Arithmetic operators
- Logical operators
- Relational operators
- Bitwise operators
- Assignment operators
- Ternary/conditional operator
- Type Operator
Arithmetic operators
The arithmetic operator is responsible for performing arithmetical operations. It takes numerical values as input, operates on them, and returns the output as a single value
Operator Name | Description |
+ Addition operator |
It takes two or more operands and performs addition. If the given arguments are of type string, it concatenates them instead. //Addition let num1=23; let num2=2 let sum = num1+num2; console.log(sum) //25; //String concatenation let str1 ="Hello" let str2 = "Typescript" let add = str1+ " "+str2 console.log(add) //"Hello Typescript" |
- Subtraction operator.
|
It takes two or more operands and performs subtraction. //Subtraction let num1=23; let num2=2 let diff= num1-num2; console.log(diff) //21; |
/ Division operator. |
It takes two operands as numerator and denominator, then performs division. //Division let num=40; let den=4; console.log(num/den) //10; |
* Multiplication operator.
|
It takes two or more operands and performs Multiplication. //Multiplication let num1=2; let num2=4; let mult= num1*num2; console.log(mult) //8; |
% Remainder operator
|
It is also known as modulus operator and is responsible for performing division then returning the remainder. //Remainder let num1=5; let num2=4; let remainder= num1%num2; console.log(remainder); //1 |
** Exponentiation operator.
|
It takes two operands and raises the power of the left operand by the value of the right operand. //Exponent let num1=2; let num2=3; let ans= num1**num2; console.log(ans); //2*2*2=8 |
++ Increment operator
|
It takes a single operand and increases its value by 1. let num=2; console.log(num); //2 num++; console.log(num); //3 ++num; console.log(num); //4 |
-- Decrement operator
|
It takes a single operand and decreases its value by 1. let num=10; console.log(num); //10 num-- console.log(num); //9 --num; console.log(num); //8 |