Home  >  Article  >  Web Front-end  >  How to implement power calculation in javascript

How to implement power calculation in javascript

青灯夜游
青灯夜游Original
2021-10-20 15:34:5012307browse

In JavaScript, you can use the pow() method of the Math object to implement power calculations. "Math.pow()" is an exponentiation function, and the syntax is "Math.pow(x,y)" , can return the value of x raised to the power of y.

How to implement power calculation in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

In JavaScript, if you want to perform power calculations, you can use the pow() method of the Math object. Math.pow() is the exponentiation function

The syntax is

Math.pow(x,y)
  • x Required. base. Must be a number.​

  • #y Required. Power number. Must be a number.

can return a value representing x raised to the power of y.

Example:

console.log(Math.pow(0, 1));
console.log(Math.pow(1, 0));
console.log(Math.pow(1, 1));
console.log(Math.pow(1, 10));
console.log(Math.pow(3, 3));
console.log(Math.pow(-3, 3));
console.log(Math.pow(2, 4));

How to implement power calculation in javascript

Note: If the result is an imaginary or negative number, this method will return NaN. If a floating point overflow occurs due to an exponent that is too large, this method returns Infinity.

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to implement power calculation in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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