Javascript method for exponentiation: 1. Use the pow() method of the Math object, the syntax "Math.pow(n, m)", which can return the value of n raised to the power of m; 2. Use The exponentiation operator "**", with the syntax "x ** y", can return the value of x raised to the y power.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Exponentiation operation
Power (power) is the result of exponential operation. When m is a positive integer, nᵐ means that the meaning of this formula is the multiplication of m n. When m is a decimal, m can be written as a/b (where a and b are integers), and nᵐ means nᵃ is raised to the bth root.
When m is an imaginary number, you need to use Euler's formula eiθ=cosθ isinθ, and then use the logarithmic properties to solve it.
Considering nᵐ as the result of exponentiation, it is called n raised to the m power, also called n raised to the m power.
How to perform exponentiation operation in javascript
Method 1: Use pow() method
The pow() method returns the value of n raised to the power m (nᵐ). If the result is imaginary or negative, the method returns NaN. If a floating point overflow occurs due to an exponent that is too large, this method returns Infinity.
Example: Return the value of 4 raised to the third power (4*4*4)
console.log(Math.pow(4, 3));
Method 2: Use the exponentiation operator (**
)
The exponentiation operator (**) returns the first operand as the base, and the second operand as the power of the exponent. It is equivalent to Math.pow
, except that it also accepts BigInts as operands.
console.log(3 ** 4); // expected output: 81 console.log(10 ** -2); // expected output: 0.01 console.log(2 ** 3 ** 2); // expected output: 512 console.log((2 ** 3) ** 2); // expected output: 64
【Related recommendations: javascript learning tutorial】
The above is the detailed content of How to perform exponentiation operation in javascript. For more information, please follow other related articles on the PHP Chinese website!