JavaScript metrics

How do you execute exponents in JavaScript?

How would you do 12 ^ 2?

+82
javascript math
May 6 '11 at 5:16
source share
6 answers

There is an exponentiation operator that is part of the final ES7 specification. It is supposed to work similarly with python and matlab:

a**b // will rise a to the power b 

This is now implemented in Edge14, Chrome52 , and is also available with traceur or babel.

+22
Nov 18 '15 at 20:04
source share

Math.pow() :

 js> Math.pow(12, 2) 144 
+118
May 6 '11 at 5:18 a.m.
source share

Math.pow(base, exponent) , for starters.

Example:

 Math.pow(12, 2) 
+23
May 6 '11 at 5:19
source share

Math.pow(x, y) works fine for x ^ y and even evaluates an expression when y is not an integer. A piece of code that does not rely on Math.pow , but which can only evaluate integer metrics:

 function exp(base, exponent) { exponent = Math.round(exponent); if (exponent == 0) { return 1; } if (exponent < 0) { return 1 / exp(base, -exponent); } if (exponent > 0) { return base * exp(base, exponent - 1) } } 
+7
Jun 08 2018-11-11T00:
source share

How we perform metrics in JavaScript
According to MDN
The exponentiation operator returns the result of raising the first operand to the second operand of a degree. That is, var1 var2 in the previous expression, where var1 and var2 are variables. The exponentiation operator is associative on the right: a ** b ** c is equal to a ** (b ** c).
For example:
2**3 // here 2 is multiplied 3 times by 2, and the result will be 8.
4**4 // here 4 multiplies 4 times by 4, and the result is 256.

0
Jan 10 '19 at 9:05
source share

If you want to find the strength x of y, then

use Math.pow

Example

 document.write(Math.pow(7,2)); 

If you want the exponent to use Math.exp

Example

 document.write(Math.exp(1)); 
-6
May 6 '11 at 5:22
source share



All Articles