Skip to main content
The /= (division assignment) operator divides the value of the left operand by the value of the right operand and assigns the resulting quotient to the left operand.
While functionally similar to x = x / y, the /= operator evaluates the left operand x exactly once. This distinction is critical when the left operand is a property access expression with side effects (e.g., getObj().prop /= 2 invokes getObj() only once, whereas getObj().prop = getObj().prop / 2 invokes it twice).

Type Constraints

In TypeScript, the compiler enforces strict type rules for the /= operator:
  • Operand Types: Both operands must resolve to type number, bigint, a numeric enum, or any.
  • Type Homogeneity: You cannot mix bigint with number or enum types. However, number and numeric enum types are interoperable and can be freely mixed in the same operation.
  • Mutability: The left operand must be a mutable reference (e.g., a variable declared with let, var, or a writable object property).

Syntax Visualization

Standard Number and Enum Division:
BigInt Division:
TypeScript Compiler Errors:

Runtime Evaluation Rules

While TypeScript handles compile-time type checking, the runtime execution follows standard IEEE 754 rules inherited from JavaScript:
  • Division by Zero (number): If the right operand is 0, the operation evaluates to Infinity, -Infinity, or NaN (if the left operand is also 0), and assigns this value to the left operand.
  • Division by Zero (bigint): If the right operand is 0n, the operation throws a runtime RangeError: Division by zero.
Tired of Poor TypeScript Skills? Fix That With Deep Grasping!Learn More