Skip to content
Tutorials Center
My learning

Topics

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.

JavaScript
Math.pow(base, exponent)
base ** exponent

Try it

JS Powers, roots and negatives

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Precedence and BigInt

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
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 ** c means a ** (b ** c).
  • Unary minus needs parentheses. -2 ** 2 is a SyntaxError, because it is ambiguous. Write (-2) ** 2 or -(2 ** 2).
  • Anything to the power 0 is 1, even NaN ** 0. 1 ** Infinity and 1 ** NaN are NaN.
  • Negative base with a fractional exponent is NaN (no real result). That includes cube roots of negative numbers: use Math.cbrt() for those.
  • Zero: 0 ** -1 is Infinity, and 0 ** 0 is 1.
  • Floating-point error. Fractional powers are rarely exact, so 64 ** (1 / 3) is 3.9999999999999996 rather than 4. Round for display or use Math.cbrt() and Math.sqrt().
  • Integer powers of integers are exact while the result stays below Number.MAX_SAFE_INTEGER; beyond that use BigInt.
  • Square roots and cubes: Math.sqrt(x) is clearer and a bit more precise than x ** 0.5.

Browser support

Math.pow() works everywhere. The ** operator and **= are part of ES2016 and supported in all current browsers.