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 (**=) evaluates the result of raising the first operand (the base) to the power of the second operand (the exponent) and assigns that evaluated value back to the first operand.
LeftHandSideExpression **= AssignmentExpression
Mechanically, x **= y is equivalent to x = x ** y, except that the LeftHandSideExpression is evaluated only once. This distinction is relevant when the left side involves complex property accessors or getter/setter side effects.

Type Coercion and Evaluation

Before the exponentiation operation occurs, JavaScript applies the ToNumeric abstract operation to both operands.
  • If the operands are strings, booleans, or objects, they are implicitly coerced into numeric types (Number or BigInt). For example, an object whose valueOf() method returns a BigInt will be coerced into a BigInt, whereas a string like "5" will be coerced into a Number.
  • The operator supports both Number and BigInt primitives.
  • The resulting assigned value will match the type of the operands after they have been coerced to numeric types, not their original evaluated type.
// Standard Number evaluation
let base = 3;
base **= 4; 
console.log(base); // 81

// BigInt evaluation
let bigBase = 2n;
bigBase **= 3n; 
console.log(bigBase); // 8n

// Implicit Type Coercion
let strBase = "5";
strBase **= 2; // String "5" is coerced to Number 5
console.log(strBase); // 25
console.log(typeof strBase); // "number" (matches the coerced type, not the original String)

Associativity

Like all assignment operators in JavaScript, **= is right-associative. When chaining multiple assignment operators, evaluation proceeds from right to left.
let a = 2;
let b = 3;
a **= b **= 2; 

// Evaluates as: a **= (b **= 2)
// b becomes 9
// a becomes 2 ** 9
console.log(a); // 512
console.log(b); // 9

Exceptions and Edge Cases

  • Type Mixing: Attempting to mix BigInt and Number operands without explicit conversion will throw a TypeError.
  • Negative Bases with Fractional Exponents: If the base is a negative number and the exponent is a fraction, the operation yields NaN because JavaScript does not support imaginary numbers in standard numeric types.
let mixedBase = 5n;
// mixedBase **= 2; // Throws TypeError: Cannot mix BigInt and other types

let negBase = -4;
negBase **= 0.5; // Equivalent to the square root of -4
console.log(negBase); // NaN
Master JavaScript with Deep Grasping Methodology!Learn More