Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between ECMAScript 2016 exponentiation operator and Math.pow()

What is the benefit to using the ECMAScriptย 2016 exponentiation operator over the current Math.pow()? In other words, besides reducing key strokes, what is the difference between

Math.pow(2, 2) => 4 and 2 ** 2 => 4

like image 258
Matt Avatar asked Jun 02 '16 20:06

Matt


People also ask

What does Math pow () do?

The Math. pow() method returns the value of x to the power of y (xy).

What does POW () mean in JavaScript?

pow() The Math.pow() method returns the value of a base raised to a power. That is. ๐™ผ๐šŠ๐š๐š‘.๐š™๐š˜๐š  ( ๐šก , ๐šข ) = x y.

Is Math POW a double?

pow() is used to return the value of first argument raised to the power of the second argument. The return type of pow() method is double.

What is the use of Math POW in Java?

pow() is used to calculate a number raise to the power of some other number. This function accepts two parameters and returns the value of first parameter raised to the second parameter.


2 Answers

None. As you can read in the ES7 spec, both Math.pow and the ** exponentation operator cast their arguments/operands to numbers and use the very same algorithm to determine the result.

Addendum: this changed with the introduction of the BigInt type in ES2020, whose values are only supported by operators (including **) but not the Math object.

like image 177
Bergi Avatar answered Sep 21 '22 04:09

Bergi


Late to the party -- I just wanted to add that as much as there is no difference between the two ways, I recently came to realize that the ** exponentiation operator isn't supported in Internet Explorer, so developers that are interested in extensive cross-browser support for their applications, may prefer to choose the Math.pow(...) over the exponentiation operator.enter image description here

like image 36
Shimi Avatar answered Sep 24 '22 04:09

Shimi