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
W3Schools.com
Math.pow() is an ECMAScript1 (JavaScript 1997) feature.
Discussions

algorithm - JavaScript implementation of Math.pow - Stack Overflow
Here's a tail recursive ES2015 solution: const power = (base, exp, acc = 1) => exp === 0 ? acc : power(base, exp - 1, base * acc). ... Take a look at iterative (non recursive) approaches on integer math for some additional ideas: Power by squaring for negative exponents More on stackoverflow.com
🌐 stackoverflow.com
JavaScript Exponents Explained - Math.pow() Examples
Math Pow Math.pow() returns the value of a number to the power of another number. Syntax Math.pow(base, exponent) , where base is the base number and exponent is the number by which to raise the base . pow() is a static method of Math , therefore it is always called as Math.pow() rather than ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
3
August 5, 2019
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 28, 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
🌐
Math.js
mathjs.org › docs › reference › functions › pow.html
math.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 › difference-between-math-pow-and-in-javascript
Difference Between Math.pow() and ** in JavaScript - GeeksforGeeks
July 23, 2025 - The ** operator is the exponentiation operator introduced in the ECMAScript 2016 (ES7). The exponentiation operator ** returns the result of the raising the base to the power of the exponent similar to the Math.pow().
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);
}

🌐
Programiz
programiz.com › javascript › library › math › pow
JavaScript Math.pow() (with Examples)
The pow() method computes the power of a number by raising the second argument to the power of the first argument.
Find elsewhere
🌐
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.
🌐
HCL Software
help.hcl-software.com › dom_designer › 9.0.1 › reference › r_wpdr_standard_math_pow_r.html
pow (JavaScript)
Gets a number raised to the exponential power of another number. Math (JavaScript) pow(x:double, y:double) : double · Special cases are as follows: The following yield NaN: exponent NaN · base NaN and exponent not 0 · base 1 and exponent Infinity · base less than 0 and exponent not integer ·
🌐
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.
🌐
freeCodeCamp
forum.freecodecamp.org › guide
JavaScript Exponents Explained - Math.pow() Examples
August 5, 2019 - Math Pow Math.pow() returns the value of a number to the power of another number. Syntax Math.pow(base, exponent) , where base is the base number and exponent is the number by which to raise the base . pow() is …
🌐
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.
🌐
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 −
🌐
Edgecompute
js-compute-reference-docs.edgecompute.app › math.pow()
Math.pow() | @fastly/js-compute
The Math.pow() method returns the value of a base raised to a power.
🌐
Reality Ripple
udn.realityripple.com › docs › Web › JavaScript › Reference › Global_Objects › Math › pow
Math.pow() - JavaScript
A number representing the given base taken to the power of the given exponent. The Math.pow() function returns the base to the exponent power, that is, baseexponent, the base and the exponent are in decimal numeral system.
🌐
DEV Community
dev.to › mayallo › exponentiation-in-javascript-a-beginners-guide-5gdj
Exponentiation in JavaScript: A Beginner’s Guide - DEV Community
July 8, 2023 - If, for instance, we raise 2 to the power of 3, we calculate it as 2 * 2 * 2, which gives us the result of 8. In JavaScript, you can use either the ** operator introduced in ES6 or the method Math.pow() when evaluating exponents.
🌐
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.
🌐
CoreUI
coreui.io › answers › how-to-raise-a-number-to-a-power-in-javascript
How to raise a number to a power in JavaScript · CoreUI
September 28, 2025 - Use Math.pow() or the exponentiation operator (**) to raise numbers to powers efficiently in JavaScript.
🌐
Dustin John Pfister
dustinpfister.github.io › 2019 › 12 › 10 › js-math-pow
Math pow in javaScript | Dustin John Pfister at github pages
August 12, 2021 - The Math pow method is what can be used in javaScript to create a number that is a power from a base and an exponent, also know as Exponentiation. The use of this will come up often when working out e
🌐
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.
🌐
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.