42

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

Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
Matt
  • 4,462
  • 5
  • 25
  • 35
  • 1
    From a search around, it appears to be for no other reason than it looks better. There's also the potential for using the `**=` operator too. – slugonamission Jun 02 '16 at 20:12

3 Answers3

46

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
5

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

Shimi
  • 1,178
  • 8
  • 18
0

Math.pow(2,2) === 2**2; // FALSE

Math.pow(99,99);
99 ** 99;

Result:

3.697296376497263e+197

3.697296376497268e+197

Gormonn
  • 31
  • 5
  • Either this was fixed since then or it's implementation-dependent, because I just checked in my browser console (Firefox on macOS) and I get the same value for both of those. – John Montgomery Dec 08 '20 at 18:45
  • 4
    It was fixed. See https://bugs.chromium.org/p/v8/issues/detail?id=5848 for example – Grant Gryczan Jan 02 '21 at 20:20