Brief :
use parseInt or Math.floor to have y/2 as integer, unleness you will not reach 0 which is the stopper of recursion .
Details
if you want to transalte [C Algo]:
int power(int x, unsigned int y)
{
if( y == 0)
return 1;
else if (y%2 == 0)
return power(x, y/2)*power(x, y/2);
else
return x*power(x, y/2)*power(x, y/2);
}
To [JS Algo] , you will have :
function power(x,y){
if(y===0){return 1}
else if (y%2 ===0){
return power(x,parseInt(y/2))*power(x,parseInt(y/2))
}else{
return x*power(x,parseInt(y/2))*power(x,parseInt(y/2))
}
}
DEMO :
function power(x,y){
if(y===0){return 1}
else if (y%2 ===0){
return power(x,parseInt(y/2))*power(x,parseInt(y/2))
}else{
return x*power(x,parseInt(y/2))*power(x,parseInt(y/2))
}
}
console.log(power(3,2))
Answer from Abdennour TOUMI on Stack OverflowMDN Web Docs
developer.mozilla.org โบ en-US โบ docs โบ Web โบ JavaScript โบ Reference โบ Global_Objects โบ Math โบ pow
Math.pow() - JavaScript | MDN
The Math.pow() static method returns the value of a base raised to a power. That is
W3Schools
w3schools.com โบ jsref โบ jsref_pow.asp
JavaScript Math pow() Method
The Math.pow() method returns the value of x to the power of y (xy).
Videos
01:58
JavaScript Basics - Math.pow() - YouTube
Math.pow() function in JavaScript | pow() method in JavaScript ...
03:17
Math Pow JavaScript - YouTube
01:34
How to calculate power of a number in javascript Math.pow() and ...
JavaScript Math Pow | JavaScript Tutorial For Beginners - YouTube
00:53
JavaScript Math pow() method - JavaScript Tutorial for Beginners ...
algorithm - JavaScript implementation of Math.pow - Stack Overflow
It will give you the same result of JavaScript build in method ( Math.pi(x, y)) but the only problem is you can't use Power as decimal number. More on stackoverflow.com
Using a loop instead of math.pow()
What are the constraints on exponent? Can it be zero? Can it be negative? Can it be non-integer? More on reddit.com
Math.pow(-2, 1.5) = NaN
Ask yourself what number, when squared makes -4. More on reddit.com
What does Math.Pow(x,2) do under the hood, that is much slower than x*x?
Math.Pow needs to be able to raise any number to the power of any other number, not just 2. It's instrinsically a much slower computation than just multiplying one number by another. CPUs generally don't even have a single instruction for raising an arbitrary number to an arbitrary power - they have instructions for taking natural logarithms, and raising e to the power of some number, so they combine them to do: y = e^(n*ln(x)) More on reddit.com
What does math pow in JavaScript return?
It returns a number. The value equals the base raised to the exponent. If you write Math.pow(4, 2), it gives 16.
flatcoding.com
flatcoding.com โบ home โบ math pow in javascript : how to raise numbers to power
Math pow in JavaScript : How to Raise Numbers to Power - FlatCoding
Does math pow work with zero values?
Yes. If the exponent is 0, the result is always 1. If the base is 0 and the exponent is more than 0, the result is 0.
flatcoding.com
flatcoding.com โบ home โบ math pow in javascript : how to raise numbers to power
Math pow in JavaScript : How to Raise Numbers to Power - FlatCoding
Can I use decimal exponents with math pow?
Yes. You can write decimal values. For example, Math.pow(25, 0.5) returns 5. That is the square root of 25.
flatcoding.com
flatcoding.com โบ home โบ math pow in javascript : how to raise numbers to power
Math pow in JavaScript : How to Raise Numbers to Power - FlatCoding
Math.js
mathjs.org โบ docs โบ reference โบ functions โบ pow.html
math.js | an extensive math library for JavaScript and Node.js
math.pow(2, 3) // returns number 8 const a = math.complex(2, 3) math.pow(a, 2) // returns Complex -5 + 12i const b = [[1, 2], [4, 3]] math.pow(b, 2) // returns Array [[9, 8], [16, 17]] const c = [[1, 2], [4, 3]] math.pow(c, -1) // returns Array [[-0.6, 0.4], [0.8, -0.2]]
Programiz
programiz.com โบ javascript โบ library โบ math โบ pow
JavaScript Math.pow() (with Examples)
Become a certified JavaScript programmer. Try Programiz PRO! ... The pow() method computes the power of a number by raising the second argument to the power of the first argument. // computes 52 let power = Math.pow(5, 2); console.log(power); // Output: 25
Top answer 1 of 4
7
Brief :
use parseInt or Math.floor to have y/2 as integer, unleness you will not reach 0 which is the stopper of recursion .
Details
if you want to transalte [C Algo]:
int power(int x, unsigned int y)
{
if( y == 0)
return 1;
else if (y%2 == 0)
return power(x, y/2)*power(x, y/2);
else
return x*power(x, y/2)*power(x, y/2);
}
To [JS Algo] , you will have :
function power(x,y){
if(y===0){return 1}
else if (y%2 ===0){
return power(x,parseInt(y/2))*power(x,parseInt(y/2))
}else{
return x*power(x,parseInt(y/2))*power(x,parseInt(y/2))
}
}
DEMO :
function power(x,y){
if(y===0){return 1}
else if (y%2 ===0){
return power(x,parseInt(y/2))*power(x,parseInt(y/2))
}else{
return x*power(x,parseInt(y/2))*power(x,parseInt(y/2))
}
}
console.log(power(3,2))
2 of 4
2
Try this out
It will give you the same result of JavaScript build in method ( Math.pi(x, y)) but the only problem is you can't use Power as decimal number.
Math.my_pow = (x, y) => {
if (typeof x != "number" || typeof y != "number")
throw "(x) and (y) should only be number";
if (y == 0) return 1;
if (x == 0 && y > 0 ) return 0;
const base = x;
var value = base;
var pow = y;
if (y < 0) pow = y * -1;
for (var i = 1; i < pow; i++) {
value *= base;
}
if (y < 0) return 1 / value;
return value;
};
try {
console.log( Math.my_pow(0, -3) );
console.log( Math.pow(0, -2) );
console.log( Math.my_pow(-5, -3) );
console.log( Math.pow(-5, -3) );
console.log( Math.my_pow(8, -7) );
console.log( Math.pow(8, -7)) ;
} catch (err) {
console.log(err);
}
MDN Web Docs
developer.mozilla.org โบ en-US โบ docs โบ Web โบ JavaScript โบ Reference โบ Operators โบ Exponentiation
Exponentiation (**) - JavaScript | MDN
The exponentiation (**) operator returns the result of raising the first operand to the power of the second operand. It is equivalent to Math.pow(), except it also accepts BigInts as operands.
TutorialsPoint
tutorialspoint.com โบ home โบ javascript โบ javascript math.pow() function
JavaScript Math.pow() Function
September 1, 2008 - This method returns the value of x to the power of y (xy). In the following example, we are using the JavaScript Math.pow() method with postive and negative integer arguments โ
W3Schools
w3schools.com โบ js โบ js_math.asp
JavaScript Math Object
Math.pow(x, y) returns the value of x to the power of y: Math.pow(8, 2); Try it Yourself ยป ยท Math.sqrt(x) returns the square root of x: Math.sqrt(64); Try it Yourself ยป ยท Math.abs(x) returns the absolute (positive) value of x: Math.abs(-4.7); ...
Code.mu
code.mu โบ en โบ javascript โบ manual โบ math โบ Math.pow
The Math.pow method - a power of a number in JavaScript
The Math.pow method raises a number to a given power. The first parameter is the number, the second is the power to raise it to in JavaScript.
GeeksforGeeks
geeksforgeeks.org โบ javascript โบ difference-between-math-pow-and-in-javascript
Difference Between Math.pow() and ** in JavaScript - GeeksforGeeks
July 23, 2025 - The Math.pow() is a function provided by the Math object in JavaScript to perform the exponentiation.
University of Houston-Clear Lake
sceweb.uhcl.edu โบ helm โบ WEBPAGE-Javascript โบ my_files โบ Object โบ Module-7 โบ Mrthod โบ javascript__math_pow_method.html
JavaScript - Math pow Method
Math.pow(base, exponent ) ; base โ The base number. exponents โ The exponent to which to raise base. Returns the base to the exponent power, that is, baseexponent. Try the following example program. <html> <head> <title>JavaScript Math pow() Method</title> </head> <body> <script ...
TechOnTheNet
techonthenet.com โบ js โบ math_pow.php
JavaScript: Math pow() function
In JavaScript, pow() is a function that is used to return m raised to the nth power. Because the pow() function is a static function of the Math object, it must be invoked through the placeholder object called Math.