Math.pow and the ** Operator
What you'll learn: How to raise numbers to powers using two interchangeable methods in JavaScript.
The Core Idea
When you need to calculate exponents—like 2 to the power of 3 (2³ = 8)—JavaScript gives you two ways to do it: the traditional Math.pow() method and the modern ** operator. Both produce identical results, but the operator syntax is more concise and readable.
Using Math.pow()
Math.pow(base, exponent) takes two arguments: the base number and the exponent:
Math.pow(2, 3); // 8 (2 × 2 × 2)
Math.pow(5, 2); // 25 (5 × 5)
Math.pow(10, -2); // 0.01 (1 / 10²)
Math.pow(16, 0.5); // 4 (square root)
Math.pow(2, 0); // 1 (anything to power 0 is 1)
Using the ** Operator
The ** operator does exactly the same thing with cleaner syntax:
2 ** 3; // 8
5 ** 2; // 25
10 ** -2; // 0.01
16 ** 0.5; // 4
2 ** 0; // 1
Which Should You Use?
The ** operator is preferred in modern JavaScript because it's:
- More concise and readable
- Consistent with mathematical notation
- Easier to chain in expressions
// Cleaner than Math.pow(Math.pow(2, 3), 2)
(2 ** 3) ** 2; // 64
// Natural in calculations
let area = Math.PI * radius ** 2;
Use Math.pow() only when working with older codebases or if you need consistency with other Math methods.
Key Takeaway: Both Math.pow(base, exponent) and base ** exponent calculate powers identically; prefer the ** operator for cleaner, more modern code.