JavaScript Reference ยท Math
Math.pow() and the ** operator
Reference for Math.pow() and the ** operator: raising numbers to powers, fractional and negative exponents, precedence rules, BigInt and edge cases.
Math.pow(base, exponent) raises base to the power of exponent. The exponentiation operator ** does the same job and is usually nicer to read: 2 ** 10 and Math.pow(2, 10) both give 1024.
Use the operator in new code. It works with BigInt values, supports the compound form **=, and keeps long formulas short. Math.pow() is still handy when you need to pass the operation around as a function.
Math.pow(base, exponent) base ** exponent
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
Math.pow(base, exponent) base ** exponent variable **= exponent
| Name | Description |
|---|---|
base |
The number to raise. |
exponent |
The power to raise it to. It may be negative or fractional. |
Return value: base raised to exponent, as a number. Results that are too large become Infinity; results too small become 0.
Exceptions: Math.pow() throws a TypeError when an argument is a BigInt. The ** operator throws a TypeError if one operand is a BigInt and the other is not, and a RangeError for a negative BigInt exponent.
Usage notes
- Right-associative.
a ** b ** cmeansa ** (b ** c). - Unary minus needs parentheses.
-2 ** 2is aSyntaxError, because it is ambiguous. Write(-2) ** 2or-(2 ** 2). - Anything to the power 0 is 1, even
NaN ** 0.1 ** Infinityand1 ** NaNareNaN. - Negative base with a fractional exponent is
NaN(no real result). That includes cube roots of negative numbers: useMath.cbrt()for those. - Zero:
0 ** -1isInfinity, and0 ** 0is1. - Floating-point error. Fractional powers are rarely exact, so
64 ** (1 / 3)is3.9999999999999996rather than4. Round for display or useMath.cbrt()andMath.sqrt(). - Integer powers of integers are exact while the result stays below
Number.MAX_SAFE_INTEGER; beyond that useBigInt. - Square roots and cubes:
Math.sqrt(x)is clearer and a bit more precise thanx ** 0.5.
Browser support
Math.pow() works everywhere. The ** operator and **= are part of ES2016 and supported in all current browsers.
Related
Math.sqrt()Math.cbrt()Math.exp()BigInt- Lesson: Arithmetic operators
- Lesson: The Math object