The javascript pow() method can come in handy in several programs dealing with mathematical expressions.
JavaScript Math pow()
JavaScript Math pow() is a built-in method used to find the value of a number raised to an exponent. To find the power of a number in Javascript, then Math.pow() function is used.
Since the Math.pow() is a static method of Math, it is always used as Math.pow() and not as a method of an object created of Math class.
Syntax
Math.pow(x, y)
Arguments
Numbers x and y, such that
x: The base of the power.
y: The exponent to which the base is raised.
Return Value
The value xy, i.e., x multiplied to itself y times.
See the following figure.
Note
- If the base is negative and the exponent is integral, the value returned is positive for even exponent values and negative for odd exponent values.
- If the base is negative and the exponent is fractional, the method will always return NaN.
- If the exponent is positive or negative zero, the method will always return 1.
Compatibility(Version and above)
- Google Chrome v1
- Internet Explorer v3
- Edge v12
- Firefox v1
- Opera v3
- Safari v1
- Android webview v1
- Chrome for Android v18
- Firefox for Android v4
- Opera for Android v10.1
- Safari on iOS v1
- Samsung Internet v1.0
- Node.js
JavaScript version: ECMAScript 1
Consider the following examples:
The following example demonstrates the use of this method with both base and exponent as non-negative integral values.
// app.js console.log(Math.pow(2, 2)); console.log(Math.pow(2, 3)); console.log(Math.pow(2, 4)); console.log(Math.pow(0, 2)); console.log(Math.pow(6, 3));
Output
node app 4 8 16 0 216
The following example demonstrates the use of this method with the exponent having a fractional value.
// app.js console.log(Math.pow(4, 0.5)); //square root of 4 console.log(Math.pow(27, 1 / 3)); //cube root of 27 console.log(Math.pow(196, 0.5)); //square root of 196 console.log(Math.pow(16, 1 / 4)); //fourth root of 16
Output
node app 2 3 14 2
Example 3
The following example demonstrates the use of this method with the exponent having a negative value.
// app.js console.log(Math.pow(1/4,-2)); console.log(Math.pow(4,-0.5));
Output
node app 16 0.5
Example 4
The following example demonstrates the use of this method, with the base having a negative value and the exponent having an integral value. In such a case, the result will have a negative value for odd exponents and a positive value for even exponents.
See the following code.
// app.js console.log(Math.pow(-2,2)); console.log(Math.pow(-2,3));
Output
node app 4 -8
Example 5
The following example demonstrates the use of this method, with the base having a negative value and the exponent having a fractional value. In such a case, this method always returns NaN.
// app.js console.log(Math.pow(-4,0.5)); console.log(Math.pow(-27,1/3)); console.log(Math.pow(-4,-0.5));
Output
node app NaN NaN NaN
Example 6
The following example demonstrates the use of this method when the exponent is positive or negative zero. In such a case, this method always returns 1.
// app.js console.log(Math.pow(2, 0)); console.log(Math.pow(2, -0));
Output
node example6 1 1
Example 7
The following example demonstrates the application of this method in a simple programming context.
Given the side of a square, find its area.
// app.js // Given the side of a square, find its area. let side; const r = require('readline'); const rl = r.createInterface({ input: process.stdin, output: process.stdout }); const prompt1 = () => { return new Promise((resolve, reject) => { rl.question('Side: ', (answer) => { side = answer; resolve(); }); }); }; const main = async () => { await prompt1(); rl.close(); var area = Math.pow(side, 2); console.log("Area: " + area); } main();
Output
Test Case 1: ->node example7 Side: 2 Area: 4 Test Case2: ->node example7 Side: 4 Area: 16