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

≯℡__Kan透↙ 提交于 2020-12-25 01:38:33

问题


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


回答1:


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.




回答2:


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

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

Result:

3.697296376497263e+197

3.697296376497268e+197




回答3:


Late to the party, 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 cross-browser support for their applications may want to choose the Math.pow(...) over the exponentiation operator.



来源:https://stackoverflow.com/questions/37601189/difference-between-ecmascript-2016-exponentiation-operator-and-math-pow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!