The exponentiation operator (**) in JavaScript is used to raise a number (the base) to the power of another number (the exponent).
This operator was introduced in ECMAScript 2016 (ES7) and provides a more concise and readable way to perform power operations compared to the Math.pow() method.
Syntax
let result = base ** exponent;
base: The number you want to raise.
exponent: The power to which you want to raise the base.
Examples
1. Basic Exponentiation
let base = 5; let exponent = 3; let result = base ** exponent; console.log(result); // Output: 125
Explanation
Here, 5 ** 3 means 5 raised to the power of 3 (5 * 5 * 5).
The result is 125.
2. Exponentiation with Negative Exponent
You can use negative numbers as exponents to find the reciprocal of the base raised to the positive exponent.
let base = 4; let exponent = -2; let result = base ** exponent; console.log(result); // Output: 0.0625
Explanation
4 ** -2 is the same as 1 / (4 ** 2), which equals 1 / 16 or 0.0625.
3. Exponentiation with Fractional Exponent
Fractional exponents can be used to find roots of numbers. For example, raising a number to the power of 0.5 is the same as finding the square root.
let base = 16; let exponent = 0.5; let result = base ** exponent; console.log(result); // Output: 4
Explanation
16 ** 0.5 is the same as finding the square root of 16, which equals 4.
4. Exponentiation with Zero Exponent
Any non-zero number raised to the power of 0 is 1.
let base1 = 10; let base2 = -5; console.log(base1 ** 0); // Output: 1 console.log(base2 ** 0); // Output: 1
Explanation
10 ** 0 and (-5) ** 0 both result in 1 because any number raised to the power of 0 equals 1.
5. Chaining Exponentiation
You can chain exponentiation operations, but be careful with the order of operations (right-associative).
let result = 2 ** 3 ** 2; console.log(result); // Output: 512
Explanation
The exponentiation operator is right-associative, so 2 ** 3 ** 2 is evaluated as 2 ** (3 ** 2).
3 ** 2 is 9, so the expression becomes 2 ** 9, which equals 512.
6. Using Exponentiation in Compound Expressions
You can use the exponentiation operator in larger expressions or combine it with other operators.
let x = 3; let y = 2; let result = 5 + x ** y * 2; console.log(result); // Output: 23
Explanation
The expression 5 + x ** y * 2 is evaluated as 5 + (3 ** 2) * 2.
3 ** 2 is 9, so the expression becomes 5 + 9 * 2.
9 * 2 is 18, and 5 + 18 is 23.
7. Exponentiation Assignment Operator (**=)
The exponentiation operator also has an assignment variant, **=, which raises the value of a variable to a specified power and reassigns it to the variable.
let number = 4; number **= 3; // Equivalent to: number = number ** 3; console.log(number); // Output: 64
Explanation
number **= 3 is shorthand for number = number ** 3.
Here, 4 ** 3 equals 64, so number is updated to 64.
Summary
The exponentiation operator (**
) in JavaScript is a concise and powerful way to raise a number to a specific power. Here’s a quick recap of its usage:
Operation | Example | Output |
---|---|---|
Basic Exponentiation | 5 ** 3 |
125 |
Negative Exponent | 4 ** -2 |
0.0625 |
Fractional Exponent | 16 ** 0.5 |
4 |
Zero Exponent | 10 ** 0 |
1 |
Right-Associative | 2 ** 3 ** 2 |
512 |
Compound Expression | 5 + 3 ** 2 * 2 |
23 |
Exponentiation Assignment | x **= 2 |
Depends on x |
By mastering the exponentiation operator, you can easily handle complex mathematical computations and make your code more readable.
It is a cleaner alternative to using Math.pow()
and supports various numerical operations such as roots, powers, and scientific calculations.