TypeScript Arithmetic Operators
Learn how to perform mathematical calculations using addition, subtraction, multiplication, division, remainder, exponentiation, increment, and decrement operators in TypeScript.
What Will We Learn?
Arithmetic operators help us calculate values and update numerical variables in TypeScript programs.
- Addition using
+. - Subtraction using
-. - Multiplication using
*. - Division using
/. - Remainder using
%. - Exponentiation using
**. - Increment and decrement using
++and--.
Arithmetic Operators Workflow
Follow these steps to understand how arithmetic calculations are performed.
Step 1
Input ValuesDeclare numeric variables.
Step 2
Apply OperatorPerform a calculation.
Step 3
Calculate ResultEvaluate the expression.
Step 4
Display ResultPrint the answer.
1. Addition Operator (+)
The addition operator + adds two numeric
values and returns their sum.
Example
let firstNumber: number = 20;
let secondNumber: number = 10;
let result = firstNumber + secondNumber;
console.log("First Number: " + firstNumber);
console.log("Second Number: " + secondNumber);
console.log("Addition: " + result);
Expected Output
First Number: 20
Second Number: 10
Addition: 30
Here, the addition operator adds 20 and
10, producing 30.
2. Subtraction Operator (-)
The subtraction operator - subtracts the
second value from the first value.
Example
let courseFees: number = 35000;
let discount: number = 5000;
let finalFees = courseFees - discount;
console.log("Course Fees: " + courseFees);
console.log("Discount: " + discount);
console.log("Final Fees: " + finalFees);
Expected Output
Course Fees: 35000
Discount: 5000
Final Fees: 30000
Subtracting the discount from the original course fees
gives the final amount of 30000.
3. Multiplication Operator (*)
The multiplication operator * multiplies
two numeric values.
Example
let monthlyFees: number = 5000;
let months: number = 6;
let totalFees = monthlyFees * months;
console.log("Monthly Fees: " + monthlyFees);
console.log("Duration: " + months + " months");
console.log("Total Fees: " + totalFees);
Expected Output
Monthly Fees: 5000
Duration: 6 months
Total Fees: 30000
Multiplying the monthly amount by six gives a total
of 30000.
4. Division Operator (/)
The division operator / divides one
number by another. The result can be an integer
or a decimal value.
Example
let totalMarks: number = 450;
let subjects: number = 5;
let averageMarks = totalMarks / subjects;
console.log("Total Marks: " + totalMarks);
console.log("Subjects: " + subjects);
console.log("Average Marks: " + averageMarks);
Expected Output
Total Marks: 450
Subjects: 5
Average Marks: 90
Example with a Decimal Result
console.log(10 / 4);
2.5
10 / 0 evaluates to Infinity.
Validate divisors when zero is not a valid input.
5. Remainder Operator (%)
The remainder operator % returns the
remainder after division.
Example
let firstNumber: number = 17;
let secondNumber: number = 5;
let remainder = firstNumber % secondNumber;
console.log("First Number: " + firstNumber);
console.log("Second Number: " + secondNumber);
console.log("Remainder: " + remainder);
Expected Output
First Number: 17
Second Number: 5
Remainder: 2
Since 17 = 5 * 3 + 2, the remainder is
2.
Practical Example: Even or Odd
let number: number = 8;
console.log(number % 2 === 0);
true
If a whole number's remainder after division by two is zero, the number is even.
6. Exponentiation Operator (**)
The exponentiation operator ** raises
a number to the power of another number.
Example
let base: number = 2;
let exponent: number = 3;
let result = base ** exponent;
console.log("Base: " + base);
console.log("Exponent: " + exponent);
console.log("Result: " + result);
Expected Output
Base: 2
Exponent: 3
Result: 8
The expression 2 ** 3 means
2 * 2 * 2, which equals 8.
7. Increment and Decrement Operators
The increment operator ++ increases a
number by one. The decrement operator --
decreases a number by one.
Example
let count: number = 5;
count++;
console.log("After Increment: " + count);
count--;
console.log("After Decrement: " + count);
Expected Output
After Increment: 6
After Decrement: 5
Prefix and Postfix
let a: number = 5;
let b: number = 5;
console.log(++a);
console.log(b++);
console.log(a);
console.log(b);
Expected Output
6
5
6
6
Prefix increment changes the value before the expression is evaluated. Postfix increment returns the current value first and then increments the variable.
8. Arithmetic Operators Comparison
| Operator | Name | Example | Result |
|---|---|---|---|
+ |
Addition | 10 + 5 |
15 |
- |
Subtraction | 10 - 5 |
5 |
* |
Multiplication | 10 * 5 |
50 |
/ |
Division | 10 / 4 |
2.5 |
% |
Remainder | 17 % 5 |
2 |
** |
Exponentiation | 2 ** 3 |
8 |
++ |
Increment | let a = 5; a++ |
a becomes 6 |
-- |
Decrement | let a = 5; a-- |
a becomes 4 |
9. Arithmetic Operator Precedence
When an expression contains multiple operators, JavaScript follows operator precedence rules. Parentheses help make the order clear.
Example
let result1 = 10 + 5 * 2;
let result2 = (10 + 5) * 2;
console.log(result1);
console.log(result2);
Expected Output
20
30
Multiplication is evaluated before addition in the first expression. In the second expression, parentheses cause addition to be evaluated first.
Practical Example: CIIT Course Fee Calculation
Let's calculate a student's final course fee using arithmetic operators.
Example
let courseFees: number = 35000;
let discount: number = 5000;
let registrationFees: number = 1000;
let discountedFees = courseFees - discount;
let totalAmount = discountedFees + registrationFees;
console.log("Original Fees: " + courseFees);
console.log("Discount: " + discount);
console.log("Discounted Fees: " + discountedFees);
console.log("Registration Fees: " + registrationFees);
console.log("Total Amount: " + totalAmount);
Expected Output
Original Fees: 35000
Discount: 5000
Discounted Fees: 30000
Registration Fees: 1000
Total Amount: 31000
The program subtracts the discount and adds the registration fees to calculate the total payable amount.
Common Mistakes
- Using the wrong operator for the intended calculation.
- Forgetting that division can produce decimal values.
- Confusing remainder with ordinary division.
- Forgetting that increment and decrement modify variables.
- Ignoring operator precedence in complex expressions.
- Performing calculations without validating input values.
Practice Task
Create a TypeScript program to calculate a student's course payment details.
- Course fees: 40000
- Discount: 6000
- Registration fees: 1500
- Number of installments: 3
Calculate the discounted fees, total payable amount, and the amount per installment.
Hint
let courseFees = 40000;
let discount = 6000;
let registrationFees = 1500;
let installments = 3;
let discountedFees = courseFees - discount;
let totalAmount = discountedFees + registrationFees;
let installmentAmount = totalAmount / installments;
console.log(discountedFees);
console.log(totalAmount);
console.log(installmentAmount);
Expected Output
34000
35500
11833.333333333334
The installment value is a decimal because the total amount is not exactly divisible by three.
Summary
In this lesson, we learned how to use arithmetic operators for addition, subtraction, multiplication, division, remainder, exponentiation, increment, and decrement. We also explored operator precedence and a practical course fee calculation.
Key Point: Arithmetic operators help calculate and update numeric values in TypeScript applications.