Math.pow(base, exponent) raises a number to a power. Modern JavaScript also provides the exponentiation operator **, which is usually shorter and easier to read.
console.log(Math.pow(2, 5)); // 32
console.log(2 ** 5); // 32Math.pow(base, exponent)console.log(Math.pow(5, 5)); // 3125
console.log(Math.pow(2, 2)); // 4
console.log(Math.pow(9, 0.5)); // 3console.log(Math.pow(2, -2)); // 0.25
console.log(10 ** -3); // 0.001console.log(Math.pow(-2, 2)); // 4
console.log(Math.pow(-2, 3)); // -8
console.log(Math.pow(-2, 0.5)); // NaN: no real square rootconsole.log(Math.pow(0, 0)); // 1
console.log(Math.pow(0, 1)); // 0
console.log(Math.pow(1, 0)); // 1For Number values, the two forms are generally equivalent. The ** operator also works with BigInt when both operands are BigInts, while Math.pow() only works with numbers.
const square = 12 ** 2;
const cube = 12 ** 3;
console.log(square, cube); // 144 1728for (let exponent = 0; exponent < 5; exponent += 1) {
const row = [];
for (let base = 0; base < 5; base += 1) {
row.push(Math.pow(base, exponent));
}
console.log(row.join(", "));
}See also Math.exp().
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.