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 *= (multiplication assignment) operator multiplies the value of the left operand by the value of the right operand and assigns the computed product to the left operand.
leftOperand *= rightOperand;

Technical Mechanics

  1. Single Evaluation: The reference to the left operand is evaluated exactly once. This is a critical distinction from the expanded form leftOperand = leftOperand * rightOperand, which evaluates the left side twice. If the left operand contains an expression with side effects (such as arr[i++]), the *= operator ensures the side effect executes only once.
  2. Mutability Requirement: The left operand must be a valid, mutable l-value, such as a variable declared with let or var, or a writable object property. Attempting to use *= on a const declaration will result in a compiler error.
  3. Return Value: The operation returns the newly assigned value of the left operand, allowing the assignment to be chained or embedded within larger expressions.

TypeScript Type Constraints

TypeScript enforces strict compile-time type checking on both operands involved in the *= operation:
  • Permitted Types: Both operands must resolve to type number, bigint, any, or an enum type.
  • Type Homogeneity: You cannot mix number and bigint operands. Doing so violates both TypeScript’s static type rules and JavaScript’s runtime rules.
  • Assignment Compatibility: The resulting product must be assignable back to the declared type of the left operand.

Syntax Visualization

Valid Operations:
let intVal: number = 10;
intVal *= 5; // Evaluates to 50

let bigIntVal: bigint = 10n;
bigIntVal *= 5n; // Evaluates to 50n

let anyVal: any = 10;
anyVal *= "5"; // Valid in TS due to 'any', evaluates to 50 at runtime

enum Multiplier { Base = 2 }
let enumVal: number = 10;
enumVal *= Multiplier.Base; // Evaluates to 20

// Single evaluation demonstration
let arr: number[] = [3, 4, 5];
let i: number = 0;
arr[i++] *= 2; // arr becomes [6, 4, 5], i becomes 1 (evaluated once)
TypeScript Compiler Errors:
let num: number = 10;

// Error: The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
num *= "5"; 

// Error: Operator '*=' cannot be applied to types 'number' and 'bigint'.
num *= 5n; 

const fixedNum: number = 10;

// Error: Cannot assign to 'fixedNum' because it is a constant.
fixedNum *= 5; 
Master TypeScript with Deep Grasping Methodology!Learn More