Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 1083 of the free egghead.io lessons, plus get JavaScript content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Use the Exponentiation Operator in JavaScript

3:30 JavaScript lesson by

ES2016 introduced the exponentiation operator ** to JavaScript. This lesson shows how it works and how you can use it as a replacement for the Math.pow function.


egghead.io comment guidelines

Avatar
egghead.io

ES2016 introduced the exponentiation operator ** to JavaScript. This lesson shows how it works and how you can use it as a replacement for the Math.pow function.

ECMAScript 2016 introduced a new binary arithmetic operator for exponentiation. It's written as some number ** some umber. It returns the result of raising the left-hand side to the power of the right-hand side. That is, the left operand represents the base, and the right operand represents the exponent.

Exponentiation in JavaScript follows the rules you know from mathematics. For instance, it's not commutative. After all, 10^2 is not the same as 2^10. Also, raising a value to the power of 0 always returns 1, no matter which value you choose for the base.

Note that exponents don't have to be integers. For example, you can raise a value to a power of one-half to compute a square root. You can also use negative exponents, and just like in mathematics 10^-2 is equivalent to 1/10^2.

While you don't have to parenthesize negative exponents, you do have to parenthesize negative bases. Therefore -10**2 is not a valid expression on its own. You have to parenthesize either the base -10, or the expression 10^2, depending on which of the two you actually mean.

This restriction in the JavaScript grammar makes your intent unambiguous and avoids confusion. To understand and operator, you should be aware of its precedence, and its associativity. Precedence determines the order of operations when evaluating an expression.

Just like in mathematics, exponentiation has a higher precedence than multiplication or division. This means you can write 5x10^2 and get the expected value of 500. Another way to put it is that ** binds tighter than *.

Associativity on the other hand, determines how operators of the same precedence are grouped in the absence of parentheses. The exponentiation operator is right associative, which means it's grouped from the right. Therefore 2^3^2 is implicitly grouped like this.

Notice that grouping from the left results in a different value. I recommend you always parenthesize expressions in which the exponentiation operator occurs multiple times, otherwise it's just too easy to misinterpret the meaning of the expression.

ES 2016 also specifies an operator that combined exponentiation and assignment. If you've already defined a variable, you can use = to raise it to a given power, it's the shorthand form of writing a = a2. In this way, it works like all the other arithmetic assignment operators that JavaScript defines.

Finally, let's take a look at how Babel transpiles the exponentiation operator. I've added a .babelrc configuration file, and I've also installed the ES2015 and ES2016 presets. I've also created an index.js file, which uses the exponentiation operator in a square function.

If I now run Babel, you'll see that in the generated code the exponentiation expression has been replaced by a call to the math.pow function. For any given base and exponent, the exponentiation operator and math.pow will compute the same value.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?