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 28, 2018
Math.pow(-2, 1.5) = NaN
Ask yourself what number, when squared makes -4. More on reddit.com
๐ŸŒ r/javascript
9
14
December 21, 2015
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
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
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 โˆ’
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ javascript โ€บ standard-library โ€บ Math โ€บ pow
JavaScript Math pow() - Calculate Power Value | Vultr Docs
December 2, 2024 - Zero raised to any positive power ... the base raised to the absolute value of the exponent. Use Math.pow() for calculating area or volume where exponents are involved....
๐ŸŒ
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.
๐ŸŒ
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 โ€บ home โ€บ javascript โ€บ 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, 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.