Arithmetic Operators in Typescript

TypeScript arithmetic operators are used to perform basic mathematical operations on numbers. They are similar to the arithmetic operators in JavaScript, but they offer additional features and type safety.

List of Arithmetic Operators

Here is a list of arithmetic operators available in TypeScript:

  1. Addition (+): Adds two operands together.
  2. Subtraction (-): Subtracts the second operand from the first operand.
  3. Multiplication (*): Multiplies two operands together.
  4. Division (/): Divides the first operand by the second operand.
  5. Modulo (%): Finds the remainder of dividing the first operand by the second operand.
  6. Increment (++): Increments the operand by 1.
  7. Decrement (--): Decrements the operand by 1.

Addition (+)

let sum: number = 5 + 3; // Result: 8

The addition operator (+) adds two numeric values together.

Subtraction (-)

let difference: number = 10 - 5; // Result: 5

The subtraction operator (-) subtracts the right operand from the left operand.

Multiplication (*)

let product: number = 4 * 6; // Result: 24

The multiplication operator (*) multiplies two numeric values.

Division (/)

let quotient: number = 20 / 4; // Result: 5

The division operator (/) divides the left operand by the right operand.

Modulus (%)

let remainder: number = 10 % 3; // Result: 1

The modulus operator (%) returns the remainder of the division of the left operand by the right operand.

Increment (++) and Decrement (--)

let count: number = 5; count++; // Increment: count is now 6 count--; // Decrement: count is now 5 again

The increment (++) and decrement (--) operators increase or decrease the value of a variable by 1.

Compound Assignment Operators

let total: number = 10; total += 5; // Equivalent to total = total + 5; // Result: 15 total *= 2; // Equivalent to total = total * 2; // Result: 30

Compound assignment operators combine an arithmetic operation with assignment, providing a more concise way to update variable values.

Type Safety

TypeScript's arithmetic operators ensure type safety by performing type checks on the operands. This means that you cannot use incompatible types with the operators, which prevents runtime errors. For example, you cannot add a string to a number.

Operator Precedence

Arithmetic operators have a specific order of precedence, which determines the order in which they are evaluated. For example, multiplication and division have higher precedence than addition and subtraction. You can use parentheses to override the default precedence.

Conclusion

TypeScript arithmetic operators, including addition, subtraction, multiplication, division, and modulus, facilitate mathematical operations on numeric values. Increment and decrement operators increase or decrease variables by 1, while compound assignment operators provide a concise way to perform arithmetic operations and assign the result to a variable. These operators are fundamental for numerical calculations and data manipulation in TypeScript programming.