The JavaScript Exponentiation ** operator
The exponentiation operator ** raises the left number to the power of the right. 2 ** 10 is 1024. It does the same thing as Math.pow() but reads more naturally in formulas. Use a fractional exponent for roots — 9 ** 0.5 is 3.
Overview
** is the exponentiation operator: base ** exponent raises the base to that power. 3 ** 2 is 9, 2 ** 10 is 1024. Added in ES2016, it's the concise, readable alternative to Math.pow(base, exponent) — identical in result, just nicer to read inside a larger expression.
It does everything Math.pow() does. A fractional exponent gives roots: 16 ** 0.5 is a square root (4), and 27 ** (1/3) is a cube root. A negative exponent gives a reciprocal: 2 ** -1 is 0.5.
Two syntax details. ** is right-associative, so 2 ** 3 ** 2 is 2 ** 9 (= 512), not 8 ** 2 — chains evaluate right to left. And you can't put a unary minus directly before the base without parentheses (-2 ** 2 is a syntax error); write (-2) ** 2 to make the intent clear. There's also a compound assignment form, x **= 2.
Syntax
base ** exponent
2 ** 10 // 1024
3 ** 2 // 9
16 ** 0.5 // 4 (square root)
2 ** -1 // 0.5 (reciprocal)
let x = 3; x **= 2; // x is 9
Example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
document.getElementById('out').textContent =
'2 ** 10 = ' + (2 ** 10) + '\n' +
'16 ** 0.5 = ' + (16 ** 0.5) + '\n' +
'2 ** 3 ** 2 = ' + (2 ** 3 ** 2); // 1024 / 4 / 512 (right-assoc)
</script>
Best practices
- Prefer
**over Math.pow() for inline math — it's cleaner to read. - Use a
0.5exponent for square roots, or Math.sqrt() for clarity. - Wrap a negative base in parentheses:
(-2) ** 2, since-2 ** 2is a syntax error. - Remember it's right-associative —
a ** b ** cevaluates asa ** (b ** c).
Frequently asked questions
What does the ** operator do?
2 ** 8 is 256. It's the same as Math.pow().What is the difference between ** and Math.pow()?
** is just a more concise operator form. 2 ** 3 equals Math.pow(2, 3).How do I do a square root with **?
Why is -2 ** 2 a syntax error?
(-2) ** 2 or -(2 ** 2).