Today, we’re going to explore how to write a JavaScript program to compute the power of a number
What Does “Power of a Number” Mean?
When we talk about the “power of a number,” it means raising a base number to an exponent. For example:
(2 raised to the power of 3) equals 2×2×2.
Here 2 is the base and 3 is exponent
How to do this using JAVASCRIPT??
For this, javascript has inbuilt method : Math.pov().
This takes two inputs 1)base 2)exponent
for example : Math.pow(2,3) which is equal to 2 to the power 3 ie 2*2*2 = 8
code
// Using Math.pow() to calculate the power of a number
function computePower(base, exponent) {
const result = Math.pow(base, exponent);
return result;
}
// Example usage
const base = 2;
const exponent = 3;
console.log(`${base} to the power of ${exponent} is:`, computePower(base, exponent));
Output: 2 to the power of 3 is 8