Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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.
x /= y;
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:
let num: number = 15;
num /= 3; // num is now 5

enum Divisor { Half = 2 }
let float: number = 5;
float /= Divisor.Half; // float is now 2.5 (freely mixes number and enum)
BigInt Division:
let bigNum: bigint = 10n;
bigNum /= 3n; // bigNum is now 3n (fractional components are truncated towards zero)
TypeScript Compiler Errors:
const constant: number = 10;
constant /= 2; // Error: Cannot assign to 'constant' because it is a constant.

let numVal: number = 10;
numVal /= "2"; // Error: The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.

let mixed: number = 10;
mixed /= 2n; // Error: Operator '/=' cannot be applied to types 'number' and 'bigint'.

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.
Master TypeScript with Deep Grasping Methodology!Learn More