Search code examples
javascriptexponentiationecmascript-2016

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


Solution

  • 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.