JavaScriptNumbers and Math
Math. pow()
Raises a number to a power.
- Returns
- The result of the exponentiation.
Example
#JavaScript
console.log(Math.pow(2, 10))
console.log(2 ** 10)
console.log(9 ** 0.5)
console.log(2 ** -1)Console
1024
1024
3
0.5
Definition and usage
#The Math.pow(base, exponent) function returns the base raised to the given power. Math.pow(2, 10) is 1024.
In modern JavaScript, the exponentiation operator ** gives the same result: 2 ** 10. It’s shorter and more common in new code.
The exponent can be a fraction or a negative number: a power of 0.5 is the square root, and a power of -1 is the reciprocal of the number.
Syntax
#Syntax
Math.pow(base, exponent)
base ** exponentParameters
#base
number
The base of the power.exponent
number
The exponent.
More examples
#JavaScript
const expForLevel = (level) => Math.round(100 * 1.5 ** (level - 1))
console.log([1, 2, 3, 4].map(expForLevel))Console
[100, 150, 225, 338]
Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- In new code, use the
**operator, it’s more readable. - For a square root,
Math.sqrt()is clearer than a power of 0.5.
Related entries
#Spotted a mistake or missing an example? Write to us.