ES6(+!) Every Day: ES2016’s Exponentiation Operator

A little about the series and the author

Tourné Torres
2 min readJul 12, 2017

As part of a probably very ambitious decision on my part to take advantage of the fact that I’m currently in a position to learn Javascript 15 hours a day, I’m going to do my best to digest 1 new ES6(+!) feature every 24 hours while in this program. For the most part, this is done FOR me each day with a challenging and rigorous programming curriculum striving to show both sides of the ECMA Script coin (much like what one would see when (gainfully) employed and in a position to deal with different degrees of legacy code).

What is it and what can it do?

The Exponentiation Operator is one of ECMAscript 2016’s newest features. In order to be compatible with all browsers, etc, the Babel transpiler transforms this binary arithmetic operator into a Math.pow() method when passed through it. In this case as well as in the pre-ES2016 method, the left operand becomes the base and the right becomes the exponent. The double asterisk is said to have a “tighter bind” than the single asterisk.

Math.pow(3, 4); // pre-ES2016 - returns 813 ** 4 // ES2016 - returns 81

Commutativity and Associativity

The Exponentiation Operator is NOT commutative in that the result is dependent on the order of operands.

1 ** 6 // returns 1 - not the same as...6 ** 1 // returns 6

However, it IS associative in that exponents take precedence based on their order within parenthesis. In this case, the exponentiation Operator is right-associative because expressions where it occurs multiple times is always parenthesized from the right.

6 ** 2 ** 2 // returns 1296 - is the same as...6 ** (2 ** 2) or 6 ** 4 // returns 1296

Negatives and Non-Integers

The same rules apply for using negatives and non-integers with the Exponentiation Operator as for Math.pow(), EXCEPT negative bases are ALWAYS parenthesized. This rule does not apply to negative exponents. The rule exists this way to avoid possible ambiguous intent as well as added confusion.

Non-integers are treated much the same way using the Exponentiation Operator. Exponentiation takes precedence in order of operations.

4 ** -4 // returns 0.00390625 is the same as...1/4 ** 4 // returns 0.00390625

Assign and combine

This operator also functions to assign and combine to variables in javascript.

let c = 10;c **= 2; // returns 100

Want more info? Check out these Resources:

--

--