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 current value of a variable by the value of the right operand and assigns the computed product back to the variable. It is a compound assignment operator that functions as syntactic sugar for standard multiplication and assignment.
LeftHandSideExpression *= AssignmentExpression
Conceptually, x *= y is equivalent to x = x * y, with the distinction that the LeftHandSideExpression is evaluated only once.

Evaluation Mechanics

When the JavaScript engine encounters the *= operator, it executes the following sequence according to the ECMAScript specification:
  1. Evaluates the LeftHandSideExpression to resolve its memory reference.
  2. Retrieves the current value stored at the left-hand reference.
  3. Evaluates the AssignmentExpression (the right operand) to determine its value.
  4. Applies the standard multiplication operation (*) to both values.
  5. Assigns the resulting product back to the left-hand reference.

Type Coercion

Because the underlying operation is strictly mathematical, the *= operator forces implicit numeric coercion on both operands before calculating the product. JavaScript attempts to convert non-numeric types using the internal ToNumber abstract operation.
  • Strings: Parsed for numeric literals. If a variable x holds "5", executing x *= "2" results in 10. If the string cannot be parsed (e.g., "foo"), it coerces to NaN.
  • Booleans: true coerces to 1, and false coerces to 0.
  • Null: Coerces to 0.
  • Undefined: Coerces to NaN.
let a = 5;
a *= 3; 
// a is now 15 (Standard numeric multiplication)

let b = "10";
b *= "4"; 
// b is now 40 (Both strings coerced to Numbers)

let c = 5;
c *= "hello"; 
// c is now NaN (Right operand coerced to NaN, 5 * NaN = NaN)

let d = 10;
d *= true; 
// d is now 10 (true coerced to 1)

let e = 5;
e *= null; 
// e is now 0 (null coerced to 0)

BigInt Behavior

The *= operator supports BigInt primitives, but strict type matching is required. JavaScript does not implicitly coerce between Number and BigInt. Both operands must be of the same type, otherwise the engine throws a TypeError.
let bigVal = 5n;
bigVal *= 3n; 
// bigVal is now 15n

let mixedVal = 5n;
mixedVal *= 3; 
// TypeError: Cannot mix BigInt and other types, use explicit conversions
Master JavaScript with Deep Grasping Methodology!Learn More