The Exponentiation Operator in JavaScript

in #technology7 years ago (edited)

JavaScript used to rely on Math.pow for exponentiation. Oddly enough, it did not have an easy-to-write syntax for this purpose. However, the operator ** was added recently (in ECMAScript 2016 specification) to the language and it is now available in all major browsers and NodeJS.

This is how to use it:

const x = 2 ** 5    // 32

The exponentiation operator is right associative. For example, a ** b ** c is equivalent to a ** (b ** c).

Note: You cannot use unary operators (+, -, ~, !, delete, void, typeof) immediately before the base, because that makes the expression ambiguous.

Sort:  

This syntax is much easier to use than Math.pow. I wonder if there is a performance boost, as well.

Good question. Probably there is a better performance, because the method call is removed.

So, I ran a benchmark and turns out ** is almost three times faster than Math.pow on NodeJS.

That's a very nice performance boost. It surely is an improvement.

This post has received a 7.56 % upvote from @booster thanks to: @ghasemkiani.

This wonderful post has received a bellyrub 6.92 % upvote from @bellyrub thanks to this cool cat: @ghasemkiani. My pops @zeartul is one of your top steemit witness, if you like my bellyrubs please go vote for him, if you love what he is doing vote for this comment as well.

Amazing that something like this could happen!!

Great post,thanks

This post UpVoted from @oliverbix

good pointout for new habit of writing, thanks!
upvoted

This post has been ranked within the top 50 most undervalued posts in the first half of Oct 20. We estimate that this post is undervalued by $28.46 as compared to a scenario in which every voter had an equal say.

See the full rankings and details in The Daily Tribune: Oct 20 - Part I. You can also read about some of our methodology, data analysis and technical details in our initial post.

If you are the author and would prefer not to receive these comments, simply reply "Stop" to this comment.

Coin Marketplace

STEEM 0.16
TRX 0.15
JST 0.028
BTC 58786.64
ETH 2309.42
USDT 1.00
SBD 2.49