Table of contents
Please support this book: buy it (PDF, EPUB, MOBI) or donate
(Ad, please don’t block.)

4. Exponentiation operator (**)

The exponentiation operator (**) is an ECMAScript 2016 feature by Rick Waldron.

4.1 Overview

> 6 ** 2
36

4.2 An infix operator for exponentiation

** is an infix operator for exponentiation:

x ** y

produces the same result as

Math.pow(x, y)

4.3 Examples

Normal use:

const squared = 3 ** 2; // 9

Exponentiation assignment operator:

let num = 3;
num **= 2;
console.log(num); // 9

Using exponentiation in a function (Pythagorean theorem):

function dist(x, y) {
  return Math.sqrt(x**2 + y**2);
}

4.4 Precedence

The exponentiation operator binds very strongly, more strongly than * (which, in turn, binds more strongly than +):

> 2**2 * 2
8
> 2 ** (2*2)
16

4.5 Further reading

Next: III ECMAScript 2017