Sub topic 13
Powers, Roots, and Logarithms
This chapter assumes you know the JavaScript arithmetic operators, the special values NaN and Infinity, and that numbers are stored as 64-bit floats. It covers the ** operator and the Math functions for powers, roots, exponentials, and logarithms, along with the mistakes people most often make with them.
Raising to a Power
The ** operator and Math.pow give the same result. Use ** in new code because it is shorter and also works with BigInt.
console.log(2 ** 10); // 1024
console.log(Math.pow(2, 10)); // 1024
console.log(2 ** -1); // 0.5
console.log(2 ** 3 ** 2); // 512, because ** groups right to left: 2 ** 9
console.log((-2) ** 2); // 4
console.log(0 ** 0); // 1Roots
Math.sqrt and Math.cbrt compute square and cube roots. Math.hypot returns the square root of the sum of squares, which is the length of a vector or the distance between two points.
console.log(Math.sqrt(16)); // 4
console.log(Math.sqrt(-1)); // NaN
console.log(Math.cbrt(27)); // 3
console.log(Math.cbrt(-27)); // -3
console.log((-27) ** (1 / 3)); // NaN
console.log(Math.hypot(3, 4)); // 5
console.log(16 ** 0.5); // 4A fractional exponent on a negative base gives NaN. The value 1 / 3 is not exactly one third in binary floating point, so JavaScript treats the operation as a non-integer power of a negative number. Math.cbrt handles negative inputs correctly, so use it for cube roots.
Exponentials and Logarithms
Math.exp(x) computes eˣ, and Math.log(x) is the natural logarithm, which undoes it. Math.log10 and Math.log2 give logarithms in base 10 and base 2.
console.log(Math.exp(1)); // 2.718281828459045
console.log(Math.log(Math.E)); // 1
console.log(Math.log10(1000)); // 3
console.log(Math.log2(8)); // 3
console.log(Math.log(0)); // -Infinity
console.log(Math.log(-1)); // NaNFor any other base, divide two natural logarithms. Expect rounding error in the result:
function logBase(x, base) {
return Math.log(x) / Math.log(base);
}
console.log(logBase(1000, 10)); // 2.9999999999999996
console.log(Math.log10(1000)); // 3Use the built-in log10 or log2 whenever one of them fits. If you have to floor a computed logarithm, first round it to a nearby integer when it falls within a small tolerance.
Results at the Edges
| Call | Result | Reason |
|---|---|---|
| `Math.sqrt(-4)` | `NaN` | No real square root |
| `Math.log(0)` | `-Infinity` | The logarithm grows without limit as x approaches 0 |
| `Math.log(-5)` | `NaN` | Undefined for negative numbers |
| `10 ** 400` | `Infinity` | Exceeds the largest double |
| `Math.log1p(1e-20)` | `1e-20` | Stays accurate where `Math.log(1 + 1e-20)` returns `0` |
Math.log1p(x) computes ln(1 + x), and Math.expm1(x) computes eˣ − 1. Both stay precise for very small x, which helps in financial and scientific formulas.
Exercise
Write a function digitCount(n) that returns the number of digits in a positive integer n using a logarithm, and give its result for 1000 and 999.
Show answer
function digitCount(n) { return Math.floor(Math.log10(n)) + 1; } It returns 4 for 1000 because Math.log10(1000) is exactly 3, and 3 for 999 because Math.log10(999) is about 2.9996. Math.log10 is used here instead of Math.log(n) / Math.log(10) because that division returns 2.9999999999999996 for 1000, which floors to 2 and gives the wrong answer of 3.
Exercise
Why does (-8) ** (1/3) return NaN, and what should you use instead?
Show answer
1/3 cannot be stored exactly as a binary float, so JavaScript treats the expression as a negative base raised to a non-integer power, which has no real result. Use Math.cbrt(-8), which returns -2.