How to raise a number to a power in JavaScript
Raising numbers to powers is essential for mathematical calculations, exponential growth models, scientific computations, and implementing features like compound interest calculators or geometric progressions in JavaScript applications.
With over 25 years of experience in software development and as the creator of CoreUI, I’ve implemented exponentiation extensively in components like calculators, financial tools, and data visualization features where exponential calculations are crucial for accurate mathematical operations.
From my extensive expertise, the most modern and readable approach is using the ES2016 exponentiation operator (**), while Math.pow()
remains a reliable alternative.
Both methods provide accurate results with the operator offering cleaner syntax for modern JavaScript development.
Use the exponentiation operator (**) to raise a number to a power.
const base = 2
const exponent = 3
const result = base ** exponent
// Result: 8
const powerOfTen = 10 ** 2
// Result: 100
The exponentiation operator **
calculates the result of raising the left operand to the power of the right operand. In these examples, 2 ** 3
equals 8 because 2 × 2 × 2 = 8, and 10 ** 2
equals 100 because 10 × 10 = 100. This operator handles integers, decimals, and negative exponents: 2 ** -3
equals 0.125 (1/8), and 9 ** 0.5
equals 3 (square root). The operator follows standard mathematical precedence rules and is right-associative.
Best Practice Note:
This is the same approach we use in CoreUI components for exponential calculations, growth models, and mathematical utilities across our component library.
Alternative: Math.pow(base, exponent)
provides the same functionality with function syntax. For integer exponents, both methods perform similarly. The **
operator is preferred in modern JavaScript for its clarity and conciseness. Handle potential Infinity
results for very large calculations and validate inputs to prevent unexpected outcomes.