Skip to content
Tutorials Center
My learning

Topics

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.

TypeScript
const total = 7 / 2; // 3.5 (number)
const rest = 7 % 2; // 1

Try it

TypeScript number operators

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.

TypeScript bigint, strings and errors

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

TypeScript
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: number is an IEEE 754 double, so 0.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, but Number(x) states the intent better. It does not work on bigint.
  • String concatenation precedence: 1 + 2 + "x" is "3x", whereas "x" + 1 + 2 is "x12". Template literals avoid the confusion.
  • % with negatives keeps the left sign; the mod helper above gives a mathematical modulo.
  • Dates: subtracting two Date objects is allowed by the compiler and yields milliseconds, but + on dates concatenates strings.
  • any hides mistakes: arithmetic on any compiles, so prefer unknown plus a check.