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 ** operator is the exponentiation operator in JavaScript. It evaluates to the result of raising the first operand (the base) to the power of the second operand (the exponent). It serves as the declarative, syntactic equivalent to the Math.pow() method.
base ** exponent

Evaluation Mechanics

Associativity Unlike most arithmetic operators in JavaScript (which are left-associative), the ** operator is right-associative. When multiple exponentiation operators are chained, the expression is evaluated from right to left.
2 ** 3 ** 2; 
// Evaluates as: 2 ** (3 ** 2) -> 2 ** 9
// Result: 512

(2 ** 3) ** 2; 
// Evaluates as: 8 ** 2
// Result: 64
Type Coercion The operator applies the internal ToNumeric abstract operation to both operands. If the operands are not already Numbers or BigInts, JavaScript will implicitly coerce them before evaluation.
"3" ** "2"; // 9 (Strings coerced to Numbers)
true ** 3;  // 1 (true coerced to 1)

Syntax Constraints with Unary Operators

JavaScript enforces a strict parsing rule to eliminate ambiguity regarding operator precedence between unary operators (such as +, -, ~, !, delete, void, typeof) and the exponentiation operator. You cannot place a unary operator immediately before the base operand without explicit grouping using parentheses. Doing so throws a SyntaxError.
-2 ** 2;   // SyntaxError: Unary operator used immediately before exponentiation expression
(-2) ** 2; // 4 (Base is -2)
-(2 ** 2); // -4 (Result of 2**2 is negated)

BigInt Compatibility

The ** operator natively supports BigInt operands. However, standard JavaScript type-mixing rules apply: both the base and the exponent must be of the same type. You cannot mix a Number and a BigInt.
2n ** 3n; // 8n
2n ** 3;  // TypeError: Cannot mix BigInt and other types, use explicit conversions
Note: Exponentiating a BigInt with a negative BigInt exponent throws a RangeError, as BigInts cannot represent fractional values.

IEEE 754 Edge Cases

Because JavaScript Numbers are double-precision 64-bit floats, the ** operator adheres to IEEE 754 floating-point specifications for exceptional values:
NaN ** 0;       // 1 (Any base to the power of 0 is 1)
Infinity ** 0;  // 1
2 ** NaN;       // NaN
2 ** Infinity;  // Infinity
0 ** 0;         // 1

Compound Assignment

The exponentiation operator can be combined with the assignment operator (=) to form the exponentiation assignment operator (**=), which evaluates the exponentiation and assigns the result to the left operand in a single step.
let base = 3;
base **= 3; // Equivalent to: base = base ** 3
// base is now 27
Master JavaScript with Deep Grasping Methodology!Learn More