Exponentiation Operator (**)
You'll learn how to calculate powers in JavaScript using the modern ** operator, a cleaner alternative to the older Math.pow() method.
What It Does
The exponentiation operator (**) raises a number to a power. It takes two operands: the base (left side) and the exponent (right side). The result is the base multiplied by itself as many times as the exponent indicates.
Think of it like this: 2 ** 3 means "2 to the power of 3" or "2 multiplied by itself 3 times" (2 × 2 × 2 = 8).
Basic Usage
let result = 2 ** 3;
console.log(result); // 8
let squared = 5 ** 2;
console.log(squared); // 25
let cubed = 3 ** 3;
console.log(cubed); // 27
Why Use ** Instead of Math.pow()?
Both do the same thing, but ** is more concise and reads more naturally:
// Old way
let oldWay = Math.pow(4, 2); // 16
// Modern way
let newWay = 4 ** 2; // 16
Special Cases
You can use decimals, negatives, and even fractional exponents:
let halfPower = 9 ** 0.5; // 3 (square root)
let negative = 2 ** -2; // 0.25 (same as 1 / 2²)
let decimal = 2.5 ** 2; // 6.25
In Expressions
The exponentiation operator works naturally in calculations:
let area = 3.14 * (5 ** 2); // Circle area with radius 5
console.log(area); // 78.5
Key Takeaway: Use the ** operator to calculate powers with clean, readable syntax—it's the modern replacement for Math.pow().