Home > Web Front-end > Front-end Q&A > How to perform exponentiation operation in javascript

How to perform exponentiation operation in javascript

青灯夜游
Release: 2022-02-16 16:43:57
Original
7026 people have browsed it

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.

How to perform exponentiation operation in javascript

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));
Copy after login

How to perform exponentiation operation in javascript

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
Copy after login

How to perform exponentiation operation in javascript

【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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template