TypeScript Reference ยท Type Operators
TypeScript arithmetic operators
Reference for arithmetic operators in TypeScript: + - * / % ** ++ --, number and bigint rules, string concatenation and the errors the compiler gives.
The arithmetic operators are +, -, *, /, %, **, unary + and -, and the increment and decrement operators ++ and --. TypeScript accepts them on number, bigint, any and numeric enums, and + additionally on strings, where it concatenates.
The result type follows the operands: number with number, bigint with bigint, string as soon as one side of + is a string. Mixing number and bigint is a compile error, and arithmetic on null, undefined, objects or booleans is rejected.
const total = 7 / 2; // 3.5 (number) const rest = 7 % 2; // 1
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
a + b a - b a * b a / b a % b a ** b +a -a a++ ++a a-- --a a += b a -= b a *= b a /= b a %= b a **= b
| Operator | Description |
|---|---|
+ |
Addition, or string concatenation if either operand is a string. |
-, *, / |
Subtraction, multiplication, division. Integer division does not exist for number; bigint division truncates toward zero. |
% |
Remainder with the sign of the left operand. |
** |
Exponentiation, right-associative. -2 ** 2 is a syntax error; write (-2) ** 2. |
++, -- |
Increment and decrement, prefix or postfix; operand must be a variable or property of a numeric type. |
Result type: number, bigint or string as described. Compound assignments such as += follow the same rule and require the result to be assignable to the target.
Errors: error 2365 for unsupported operand types (number + bigint), error 2362/2363 for non-numeric operands of -, * and similar, and error 2356 for ++ on a non-number. Division by zero never throws for numbers (Infinity or NaN); for bigint it throws RangeError.
Usage notes
- Floating point:
numberis an IEEE 754 double, so0.1 + 0.2 !== 0.3. Round for display, use integers (cents) for money, or a decimal library. - Integers above 2^53 lose precision; use
bigint, which has no fixed limit. - Unary
+converts strings to numbers, butNumber(x)states the intent better. It does not work onbigint. - String concatenation precedence:
1 + 2 + "x"is"3x", whereas"x" + 1 + 2is"x12". Template literals avoid the confusion. %with negatives keeps the left sign; themodhelper above gives a mathematical modulo.- Dates: subtracting two
Dateobjects is allowed by the compiler and yields milliseconds, but+on dates concatenates strings. anyhides mistakes: arithmetic onanycompiles, so preferunknownplus a check.