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 Overflow
🌐
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).
Discussions

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
🌐 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
🌐 r/javascript
10
1
June 25, 2018
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
🌐 r/AskProgramming
8
17
September 1, 2021
Is there a utility to convert from Math.pow syntax to exponentiation operator syntax?
This proposed official eslint rule links to this custom rule which says it is fixable, so if this was a JS project you could install eslint and this rule and it would auto-convert! I haven't tried using eslint rules with tslint so YMMV, but let us know what you figure out! More on reddit.com
🌐 r/typescript
8
0
August 1, 2019
People also ask

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]]
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-math-pow-method
JavaScript Math pow() Method - GeeksforGeeks
July 15, 2024 - The Math.pow() method returns a number representing the given base raised to the power of the given exponent.
🌐
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
Find elsewhere
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
July 8, 2025 - NaN ** 0 (and the equivalent Math.pow(NaN, 0)) is the only case where NaN doesn't propagate through mathematical operations — it returns 1 despite the operand being NaN. In addition, the behavior where base is 1 and exponent is non-finite (±Infinity or NaN) is different from IEEE 754, which specifies that the result should be 1, whereas JavaScript returns NaN to preserve backward compatibility with its original behavior.
🌐
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 −
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Math › pow
JavaScript Math pow() - Calculate Power Value | Vultr Docs
December 2, 2024 - JavaScript's Math.pow() function is a versatile tool used to calculate the power of a number, a task commonly encountered in scientific calculations, geometry, and general arithmetic operations.
🌐
FlatCoding
flatcoding.com › home › math pow in javascript : how to raise numbers to power
Math pow in JavaScript : How to Raise Numbers to Power - FlatCoding
June 26, 2025 - JavaScript Math.pow() raises a base to an exponent. It simplifies power math without loops. Click here to see how it works with examples.
🌐
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.
🌐
Jsremote
jsremote.jobs › tutorials › math-pow
The Math.pow() method in JavaScript? | Web developer jobs
December 30, 2022 - It allows you to calculate the power of a number. The Math.pow() function accepts two obligatory arguments. The first parameter is a base - the number for which you need the power to be computed. The second one is a number specifying the exponent.
🌐
W3Resource
w3resource.com › javascript › object-property-method › math-pow.php
JavaScript pow() Method : Math Object - w3resource
<![CDATA[ document.write(Math.pow(0,0) + "<br />"); document.write(Math.pow(0,1) + "<br />"); document.write(Math.pow(1,1) + "<br />"); document.write(Math.pow(1,100) + "<br />"); document.write(Math.pow(2,4) + "<br />"); document.write(Math.pow(-2,3) + "<br />"); document.write(Math.pow(-2,4) + "<br />"); document.write(Math.pow(-2,-2) + "<br />"); x=5 y=5 document.write(Math.pow(x,y)); //]]> </script> </body> </html> ... JavaScript Core objects, methods, properties.
🌐
Scaler
scaler.com › topics › javascript-math-pow
JavaScript Math.pow() Function - Scaler Topics
May 4, 2023 - The Math.pow() function is an in-built function in JavaScript that returns a number equal to the base number raised to its exponent. It takes two parameters - the base and the exponent.
🌐
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 ...
🌐
JavaScriptSource
javascriptsource.com › the-javascript-function-math-pow
The JavaScript function Math.pow() - JavaScriptSource
January 27, 2023 - The Math.pow() function in JavaScript is used to raise a number (called the base) to a certain power (called the exponent). The syntax for using this function is Math.pow(base, exponent).
🌐
Tutorial Gateway
tutorialgateway.org › javascript-pow
JavaScript pow Function
March 29, 2025 - If the Base or Exponent value is Null, it will return Zero. The pow function returns the Power of the given number. In this example, we will find the power of different data types and display the output.
🌐
TechOnTheNet
techonthenet.com › js › math_pow.php
JavaScript: Math pow() function
In JavaScript, the syntax for the pow() function is: ... The number used as the base in the calculation. ... The number used as the exponent in the calculation. The pow() function returns m raised to the nth power which is mn. Math is a placeholder object that contains mathematical functions ...