TypeScript Arithmetic Operators
Arithmetic operators in TypeScript are used to perform basic mathematical operations like addition, subtraction, multiplication, and division. These operators work with numbers to calculate results.
List of Arithmetic Operators
Operator | Name | Description |
---|---|---|
+ | Addition | Adds two numbers together and returns their sum. |
- | Subtraction | Subtracts the second number from the first number and returns the result. |
* | Multiplication | Multiplies two numbers together and returns the product. |
/ | Division | Divides the first number by the second number and returns the quotient. |
% | Modulus | Returns the remainder when the first number is divided by the second number. |
++ | Increment | Increases the value of a number by 1. |
-- | Decrement | Decreases the value of a number by 1. |
Examples of Arithmetic Operators
Addition (+
)
Adds two numbers together.
let a = 5;
let b = 3;
let sum = a + b;
console.log(sum); // Output: 8
Subtraction (-
)
Subtracts the second number from the first.
let a = 10;
let b = 4;
let difference = a - b;
console.log(difference); // Output: 6
Multiplication (*
)
Multiplies two numbers.
let a = 6;
let b = 7;
let product = a * b;
console.log(product); // Output: 42
Division (/
)
Divides the first number by the second.
let a = 20;
let b = 4;
let quotient = a / b;
console.log(quotient); // Output: 5
Modulus (%
)
Returns the remainder of the division of two numbers.
let a = 10;
let b = 3;
let remainder = a % b;
console.log(remainder); // Output: 1
Increment (++
)
Increases the value of a number by 1.
let a = 5;
a++;
console.log(a); // Output: 6
Decrement (--
)
Decreases the value of a number by 1.
let a = 5;
a--;
console.log(a); // Output: 4
Summary
Arithmetic operators are essential for performing calculations in TypeScript. By understanding how to use them, you can write programs that handle numbers effectively and solve various mathematical problems.