# Pow! Using exponents and roots in JavaScript

An exponent - also known as an index or a power - multiplies a base number by itself a certain number of times. root is the opposite: a number that, when multiplied by itself a certain number of times, equals a certain defined value. Exponents and roots are used constantly in everyday life, including measurement of size and volume, finance and physics simulations.

In JavaScript, exponents are represented by the wonderfully named `Math.pow()` function (short for “power”) and the new `* *` operator in ES 7.

## Pow!

To raise a number to the nth power, use the `Math.pow()` function with the base number defined first, followed by the exponent, separated by a comma. Using the console as a scratchpad:

``````Math.pow(3,2)
> 9``````

That is, 3 squared, or 3 × 3, produces 9. We can go as high as we like, of course:

``````Math.pow(5,3);
> 125``````

That is, 5 cubed, or 5 × 5 × 5, equals 125.

Exponents come in handy in all kinds of situations. A simple example would be calculating the number of seconds in an hour: `Math.pow(60,2)`.

## Root Down

`Math.sqrt()` and `Math.cbrt()` are the opposite to the `Math.pow()` function. The square root of a number is a value that, when multiplied by itself, produces that number:

``````Math.sqrt(9)
> 3``````

…while a cube root is a value multiplied by itself twice to produce a target number:

``````Math.cbrt(125)
> 5``````

## Alternate Realities

Of course, we don’t have to use whole numbers in any of the functions:

``````Math.pow(1.25, 2);
> 1.5625

Math.cbrt(56.57)
> 3.8387991760286138``````

Note that it’s entirely possible to use a negative number as the base for an exponent:

``````Math.pow(-5,2)
> 25``````

Or as the exponent itself:

``````Math.pow(10,-2)
> 0.01``````

A negative number multiplied by another will always yield a positive. However, it is never possible to express the square root of a negative as a number:

``````Math.sqrt(-9)
> NaN``````

The square roots of negatives pushes us into the world of imaginary numbers - very useful in the production of fractals - which I’ll get to in a future article.

You can use fractions in `Math.pow()` to find the square and cube roots of numbers. The square root uses an exponent of `0.5`:

``````Math.pow(5, 0.5);
> 2.23606797749979``````

The same formulation can be used to produce the cube root of a number, by using ⅓ as an exponent. Because browser support for `Math.cbrt` is limited at the moment, you may want to use the technique to produce a polyfill, as suggested by the Mozilla Developer Network site:

``````Math.cbrt = Math.cbrt || function(x) {
var y = Math.pow(Math.abs(x), 1/3);
return x < 0 ? -y : y;
};``````

## Not e

Beginners in JavaScript often understandably confuse `Math.pow()` function with `Math.exp()`, which is not an exponential function for numbers in general, but Euler’s number, the base of natural logarithms. I’ll be looking at that function, and the associated `Math.log()`, next.

Photo by Sébastien Blondeel, licensed under Creative Commons.