Open In App

JavaScript Program to Compute Power of a Number

Last Updated : 27 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will demonstrate different approaches for computing the Power of a Number using JavaScript.

The Power of a Number can be computed by raising a number to a certain exponent. It can be denoted using a^b, where the ‘a’ represents the base number & the ‘b’ represents the number to which the power is to be raised. These programs will have input numbers and power and will show the resulting output. There are various techniques to calculate the power of a number, which are described below with their illustrations.

Using JavaScript Loops

In this method, we will use JavaScript for loop to iterate and calculate output by multiplication.

Example: In this example, we will calculate the output of 5 to power 3.

Javascript
// Base number input
let n = 5

// Power input
let power = 3

// Result variable
let num = 1;
for (let i = 0; i < power; ++i) {
    num = num * n;
}

// Display output
console.log(num);

Output
125

Using Recursion

In this method, we will use a recursive function to iterate and perform multiplication at every iteration.

Example: In this example, we will calculate 8 to the power 3 using recursion.

Javascript
// Recursive function to compute Power
function pow(n, p) {
    if (p == 1) return n;
    return n * pow(n, p - 1);
}

// Base number input
let n = 8;

// Power input
let power = 3

// Display output
console.log(pow(n, power));

Output
512

Using the Math.pow() Method

This is another method to method that is used to power a number i.e., the value of the number raised to some exponent. Here, we will use this method to calculate the power of the number.

Syntax

Math.pow(base, exponent);

Example: In this example, we will calculate 7 to power 9 using Math.pow() method.

Javascript
// Base number input
let n = 7;
let power = 9;

// Calculate and display output
console.log(Math.pow(n,power));

Output
40353607

Using JavaScript Exponentiation (**) Operator

This method can also be utilized to find the power of the first operator raised to the second operator, & it is denoted by a double asterisk(**) symbol.

Syntax

Base**Power

Example: In this example, we will use the JavaScript ** operator to get 17 to power 3.

Javascript
// Base number input
let n = 17;
let power = 3;

// Calculate and display output
console.log(n**power);

Output
4913

Using Exponentiation by Squaring

Exponentiation by Squaring is an efficient method for calculating large powers of a number. It reduces the number of multiplications required by breaking down the exponentiation process into smaller parts.

Example: In this example, we will calculate 2 to the power of 10 using Exponentiation by Squaring.

JavaScript
// Function to compute power using Exponentiation by Squaring
function powerBySquaring(base, exponent) {
    if (exponent === 0) return 1;
    if (exponent % 2 === 0) {
        let halfPower = powerBySquaring(base, exponent / 2);
        return halfPower * halfPower;
    } else {
        return base * powerBySquaring(base, exponent - 1);
    }
}

// Base number input
let base = 2;

// Power input
let exponent = 10;

// Calculate and display output
console.log(powerBySquaring(base, exponent));

Output
1024




Similar Reads

JavaScript Program to Compute Iterative Power of a Number
This article will demonstrate how to compute the iterative power of a number using JavaScript. It will include iterative methods to calculate and display the power of a given number. Methods to Compute the Iterative Power of a NumberTable of Content Method 1: Using JavaScript for LoopMethod 2: Using JavaScript while LoopMethod 3: Using RecursionMet
3 min read
Javascript Program to Efficiently compute sums of diagonals of a matrix
Given a 2D square matrix, find the sum of elements in Principal and Secondary diagonals. For example, consider the following 4 X 4 input matrix. A00 A01 A02 A03 A10 A11 A12 A13 A20 A21 A22 A23 A30 A31 A32 A33 The primary diagonal is formed by the elements A00, A11, A22, A33. Condition for Principal Diagonal: The row-column condition is row = column
3 min read
JavaScript Program to Compute the Bitwise OR of all Numbers in a Range
In JavaScript, bitwise operations provide a way to manipulate individual bits of numeric values. The bitwise OR operation (|) combines the bits of two operands, resulting in a new value where each bit is set if it is set in either operand. Understanding Bitwise ORThe bitwise OR operation takes two operands and performs a logical OR operation on eac
2 min read
JavaScript program to check whether a given number is power of 2
Given a positive integer n, write a function to find if it is a power of 2 or not Examples: Input: n = 4 Output: Yes Explanation: 22 = 4 Input: n = 32 Output: Yes Explanation: 25 = 32 To solve the problem follow the below idea: A simple method for this is to simply take the log of the number on base 2 and if you get an integer then the number is th
6 min read
Compute the Bitwise XOR of all Numbers in a Range using JavaScript
The complexity of bitwise XOR computation for all the numbers in a specific range is the most frequent difficulty that many people face during the problem-solving process. Problem Description: Given two integers, L and R, find the bitwise XOR of all numbers in the inclusive range from L to R. There are several approaches to computing the bitwise XO
6 min read
Compute the Bitwise AND of all Numbers in a Range using JavaScript
The complexity of bitwise AND computation for all numbers in a specific range is a common challenge faced by developers, particularly when optimizing algorithms. We have given two integers, L and R, and find the bitwise AND of all numbers in the inclusive range from L to R in JavaScript. Example: Let's consider an example where L=3 and R=6.The bitw
3 min read
How to compute union of JavaScript arrays ?
Given two arrays containing array elements and the task is to compute the union of both arrays with the help of JavaScript. These are the methods to solve this problem which are discussed below: Table of Content Using the spread operator and setUsing the push() methodUsing filter() and concat() MethodUsing Underscore.js _.union() FunctionUsing redu
3 min read
How to compute the average of an array after mapping each element to a value in JavaScript ?
Given an array, the task is to compute the average of an array after mapping each element to a value. Input : arr=[2, 3, 5, 6, 7] Output: 4.6 Explanation : (2+3+5+6+7)/5 = 4.6 Input : [5,7,2,7,8,3,9,3] Output: 5.5 Explanation : (5+7+2+7+8+3+9+3)/8 = 5.5 Here are some common approaches: Table of Content Using foreach() loopUsing for loop with ParseI
3 min read
Php Program to Efficiently compute sums of diagonals of a matrix
Given a 2D square matrix, find the sum of elements in Principal and Secondary diagonals. For example, consider the following 4 X 4 input matrix. A00 A01 A02 A03 A10 A11 A12 A13 A20 A21 A22 A23 A30 A31 A32 A33 The primary diagonal is formed by the elements A00, A11, A22, A33. Condition for Principal Diagonal: The row-column condition is row = column
3 min read
Javascript Program for Pairs such that one is a power multiple of other
You are given an array A[] of n-elements and a positive integer k (k &gt; 1). Now you have find the number of pairs Ai, Aj such that Ai = Aj*(kx) where x is an integer. Note: (Ai, Aj) and (Aj, Ai) must be count once.Examples : Input : A[] = {3, 6, 4, 2}, k = 2 Output : 2 Explanation : We have only two pairs (4, 2) and (3, 6) Input : A[] = {2, 2, 2}
3 min read