Skip to main content

Arithmetic Operators

Introduction to Arithmetic Operators in TypeScript

In TypeScript, just like in most other programming languages, we use arithmetic operators to perform calculations. They are essential for manipulating numerical data. This tutorial is designed to help beginners understand how to use these operators effectively.

Overview of Arithmetic Operators

Arithmetic operators in TypeScript are similar to the ones you'd find in basic mathematics. They include:

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulus (%)
  • Increment (++)
  • Decrement (--)

Let's understand each of these operators in depth.

Addition Operator (+)

The addition operator adds two numerical values together. For example:

let a = 5;
let b = 2;
let sum = a + b; // sum will be 7

Subtraction Operator (-)

The subtraction operator subtracts one numerical value from another. Here's how it works:

let a = 5;
let b = 2;
let difference = a - b; // difference will be 3

Multiplication Operator (*)

The multiplication operator multiplies two numerical values. Here's an example:

let a = 5;
let b = 2;
let product = a * b; // product will be 10

Division Operator (/)

The division operator divides one numerical value by another. Here's an example:

let a = 5;
let b = 2;
let quotient = a / b; // quotient will be 2.5

Modulus Operator (%)

The modulus operator calculates the remainder of a division operation. Here's how you can use it:

let a = 5;
let b = 2;
let remainder = a % b; // remainder will be 1

This is because when 5 is divided by 2, the quotient is 2 and the remainder is 1.

Increment Operator (++)

The increment operator increases a numerical value by 1. Here's an example:

let a = 5;
a++; // a will be 6

Decrement Operator (--)

The decrement operator decreases a numerical value by 1. Here's how it works:

let a = 5;
a--; // a will be 4

As you can see, arithmetic operators in TypeScript are pretty straightforward to use. They are powerful tools that you can use to manipulate numerical data in your program.

Remember, practice makes perfect. Try to use these operators in different scenarios to understand their use-cases better. Happy coding!