Skip to main content
The **= (exponentiation assignment) operator performs exponentiation on the left operand using the right operand as the exponent, and assigns the resulting value to the left operand. It is a compound assignment operator that conceptually combines the exponentiation operation (**) with assignment (=), but guarantees that the left operand’s reference is evaluated exactly once.

Technical Characteristics

  • Type Constraints: TypeScript enforces strict type checking on this operator. Both operands must resolve to either the number type or the bigint type.
  • Type Homogeneity: The operands must be of the exact same type. Mixing number and bigint will trigger a TypeScript compiler error (TS2365) and a runtime TypeError.
  • Mutability: The left operand must be a valid, mutable l-value (e.g., a variable declared with let or var, or a mutable object property). It cannot be a const declaration or an r-value.
  • Evaluation Order: The left operand is evaluated exactly once. This is a critical semantic distinction from x = x ** y when the left operand involves a complex expression with side effects. For example, in arr[getIndex()] **= 2, the function getIndex() is executed only once, whereas arr[getIndex()] = arr[getIndex()] ** 2 would execute it twice.
  • Associativity: As an assignment operator, **= is right-associative.

Syntax Mechanics

IEEE 754 Edge Cases

When operating on number types, the operator adheres to standard IEEE 754 floating-point arithmetic rules:
  • If the right operand is NaN, the result is NaN.
  • If the right operand is 0 or -0, the result is always 1 (even if the left operand is NaN).
  • If the left operand is NaN and the right operand is not 0, the result is NaN.
Tired of Poor TypeScript Skills? Fix That With Deep Grasping!Learn More