JavaScript: Exponentiation Operator (In-Depth)

The exponentiation operator (**) is a mathematical operator that raises the left operand to the power of the right operand. Introduced in ES2016 (ES7), it is the standard way to perform exponentiation in modern JavaScript.

Basic Usage

let squared = 5 ** 2;    // 25
let cubed = 2 ** 3;      // 8
let root = 16 ** 0.5;    // 4 (square root)
    
// Output:
25
8
4

Equivalent to Math.pow()

The ** operator is functionally identical to Math.pow():

let value1 = 3 ** 4;            // 81
let value2 = Math.pow(3, 4);    // 81
    

Operator Precedence and Associativity

let result = 2 ** 3 ** 2; // 2 ** (3 ** 2) => 2 ** 9 = 512
    
// Output:
512

Negative Numbers

Use parentheses with negative bases to avoid confusion:

let a = (-2) ** 3; // -8
let b = -2 ** 3;   // - (2 ** 3) = -8
    

Assignment Operator **=

JavaScript also supports the exponentiation assignment operator (**=):

let x = 10;
x **= 2; // x = x ** 2 = 100
    
// Output:
100

Practical Examples

let arr = [1, 2, 3, 4];
let squares = arr.map(n => n ** 2); // [1, 4, 9, 16]
    

Summary

Practice: Try using ** in math-heavy code, array transformations, and interactive calculators.