The Typescript arithmetic operators take numerical values as their left & right operands, perform the arithmetic operation, and return a numerical value. The Typescript supports all the arithmetic operators like addition (+), subtraction (-), multiplication (*), division (/), etc. Note that all the numbers in Typescript are represented as IEEE 754Â floating-point numbers and use floating-point arithmetic.

Table of Contents

## Arithmetic Operators

**+** (Addition)

The addition operator (`+`

) is a binary operator, which calculates the sum of two numeric operands.

1 2 3 4 5 6 |
let a = 12 let b = 2 let c = a+b; console.log(c) //14 |

If one of the operands is a string, then the `+`

operator does a string concatenation

1 2 3 4 5 6 |
let a = "Hello" let b = 2 let c = a+b; console.log(c) //Hello2 |

1 2 3 4 5 6 7 |
let a = "12" let b = 2 let c = a+b; //string concatenation as the b is string console.log(c) //122 |

The booleans are implemented as numerical values with a single binary digit (i.e., 0 & 1). 1 is true & 0 is false.

1 2 3 4 5 6 |
let a = true //boolean let b = 2 let c = b+a; //Typescript compiler throws a warning here console.log(c) //3 because true is 1 |

1 2 3 4 5 6 7 |
let a = false //boolean let b = 2 let c = b+a; //Compiler warning console.log(c) //2 because false is 0 |

1 2 3 4 5 |
console.log(true+true+true) //3 because true is 1. //Also a compiler warning |

The Operations involving different types (example string & number) give a compiler error. This is because of Typescript type checking, which is one of the major reasons why we use typescript.

**–** (Subtraction)

The subtraction operator subtracts the right operand from the left operand. If any of the operands is not a number, then it returns a NaN

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
let a = 10 let b = 2 let c = a-b; console.log(c) //8 let d = b-a; console.log(d) //-8 //Boolean //Compiler warning console.log(true-false) //1 true is 1, false is 0 console.log(2-true) //1 |

Converts strings to numbers. Typescript compiler will flag this as an error.

1 2 3 4 5 6 7 |
let a = "1" let b = "2" console.log(a-b); //-1 //Compiler warning |

Subtraction, when one (or both) of the operand is not a number, always results in NaN

1 2 3 4 5 |
let a = "Hello" let b = 2 console.log(a-b); //NaN |

***** (Multiplication)

The multiplication operator (*) multiplies the left operand with the right operand.

1 2 3 4 5 |
let a = 5 let b = 2 console.log(a*b); //10 |

Strings are converted to numbers. Typescript compiler will flag this as an error

1 2 3 4 5 6 |
let a = "5" let b = "2" console.log(a*b); //10 |

Infinity

1 2 3 4 |
console.log(Infinity * 0) // NaN console.log(Infinity * Infinity) // Infinity |

Multiplication with non-numbers results in NaN. The Typescript will throw an error here.

1 2 3 |
console.log("Hello" * 10) // NaN //Compiler error |

**/** (Division)

The division operator (`/`

) divides the left operand (dividend) with the right operand (divisor).

Example

1 2 3 4 5 6 |
console.log(10 / 2); //5 console.log(11 / 2); //5.5 |

Strings are converted to numbers. Typescript compiler will flag this as an error

1 2 3 4 5 6 |
console.log(6 / '3'); //2 console.log('6' / '3'); //2 |

If the string is not a number, then the result is NaN.

1 2 3 4 5 |
console.log(6 / 'a'); //NaN console.log(6 / '3a'); //NaN |

Booleans are numbers. True is 1 & false is 0

1 2 3 4 5 |
console.log(6 / true); //6 true is 1 console.log(2 / false); //Infinity false is 0 |

Dividing by 0 results in Infinity

1 2 3 4 |
console.log(2 / 0); //Infinity |

**%** (modulus or Reminder)

The remainder operator (`%`

) returns the remainder leftover of a division operation between the operands. The result always takes the sign of the dividend.

1 2 3 4 5 6 7 8 |
console.log(12%5) //2 console.log(-12%5) //-2 console.log(-12%-5) //-2 console.log(12%-5) //2 |

**++** (Increment) & **—** (Decrement)

We use the increment & Decrement operators to increase or decrease the value of the variable by one. Typescript uses the `++`

(increment) & `--`

(decrement) to denote them. We can either prefix or Postfix these operators.

Increment & Decrement Operators in Typescript

**+ & –** (Unary plus & Unary minus)

The unary plus operator (`+`

) precedes its operand and converts it into a number. If it fails to convert the operand into a number, then it returns NaN. The unary (-) operator converts the operand into a number and negates it.

Read Unary plus & Unary minus operators in Typescript

****** (Exponentiation operator)

The exponentiation operator (`**`

) returns the result of raising the first operand to the power of the second operand.

1 2 3 4 5 6 7 8 9 10 11 |
console.log(3 ** 4); // 81 console.log(10 ** -2); // 0.01 console.log(2 ** 3 ** 2); // 512 console.log((2 ** 3) ** 2); // 64 console.log(2 ** (3 ** 2)); // 512 |

## Reference

**Read More**

- Complete Typescript Tutorial
- Typescript Operators
- Arithmetic Operators
- Unary plus / Unary minus Operators
- Increment/Decrement Operators
- Comparison / Relational Operators
- Equality Operator / Strict Equality Operators
- Ternary Conditional Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Nullish coalescing operator
- Comma Operator in Typescript
- Operator Precedence