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 **= (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.
leftOperand **= rightOperand;

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

// Standard numeric exponentiation
let num: number = 2;
num **= 3; 
// num evaluates to 8 (2^3)

// BigInt exponentiation
let bigNum: bigint = 3n;
bigNum **= 2n; 
// bigNum evaluates to 9n

// Complex l-value evaluation
let index = 0;
const getIndex = () => index++;
const arr = [3, 4, 5];
arr[getIndex()] **= 2; 
// arr becomes [9, 4, 5], getIndex() is called exactly once

// Invalid: Type mixing
let invalidNum: number = 5;
// invalidNum **= 2n; 
// TS Error: Operator '**=' cannot be applied to types 'number' and 'bigint'.

// Invalid: Assignment to constant
const fixedNum: number = 4;
// fixedNum **= 2; 
// TS Error: Cannot assign to 'fixedNum' because it is a constant.

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