Math.log()
Math.log(x) The Math.log() function returns the natural logarithm of a number — the logarithm to base e, where e is Euler’s number (approximately 2.718281828459045). It is the inverse of Math.exp(): if y = Math.exp(x), then x = Math.log(y).
For values of x less than or equal to 0, the result is not a real number: Math.log(0) returns -Infinity and Math.log(-1) returns NaN. For x = 1, the result is 0 (since e^0 = 1).
Syntax
Math.log(x)
Parameters
| Parameter | Type | Description |
|---|---|---|
x | number | The value to compute the natural logarithm of |
Return value
The natural logarithm of x. Returns NaN for negative inputs, -Infinity for 0, and Infinity for Infinity.
The examples below progress from straightforward Math.log() calls through edge cases and into real-world patterns — change of base, exponential equations, log returns, and entropy. Each one builds on the fundamental relationship that Math.log() is the inverse of Math.exp().
Examples
Basic usage
console.log(Math.log(1));
// 0
console.log(Math.log(Math.E));
// 1
console.log(Math.log(Math.E * Math.E));
// 2
The natural log grows slowly — doubling the input from 1 to 2 adds about 0.69, and going from 2 to e hits exactly 1. The next examples show how Math.log() behaves for larger inputs, where each tenfold increase in the argument adds roughly 2.302585 to the result.
Values greater than 1
console.log(Math.log(2));
// 0.6931471805599453
console.log(Math.log(10));
// 2.302585092994046
console.log(Math.log(100));
// 4.605170185988092
The natural log of 10 is approximately 2.302585, which is why Math.log(100) ≈ 2 * 2.302585.
Values between 0 and 1 produce negative results because you would raise e to a negative power to get a fraction. The closer the argument gets to zero, the more negative the logarithm — approaching -Infinity at the limit. This is worth keeping in mind when your input comes from a ratio or a probability that could be very small.
Values between 0 and 1
console.log(Math.log(0.5));
// -0.6931471805599453
console.log(Math.log(0.1));
// -2.302585092994046
Logarithms of values between 0 and 1 are negative, reflecting that you would raise e to a negative power to get a fraction.
The three special inputs — zero, negative numbers, and Infinity — each produce a well-defined result that matches the mathematical limit. Checking for these return values is the standard way to guard against domain errors without an extra if statement before calling Math.log().
Special values
console.log(Math.log(0));
// -Infinity
console.log(Math.log(-1));
// NaN
console.log(Math.log(Infinity));
// Infinity
The next patterns cover the most common transformations built on Math.log() — converting between bases, solving for exponents, computing financial returns, and measuring information content. Each one is a thin wrapper that keeps the core formula visible rather than burying it inside a larger function.
Common patterns
Change of base formula
const logBase10 = (x) => Math.log(x) / Math.log(10);
const logBase2 = (x) => Math.log(x) / Math.log(2);
console.log(logBase10(100));
// 2
console.log(logBase2(8));
// 3
You can compute a logarithm in any base by dividing the natural log of the value by the natural log of the base. For base 10 specifically, Math.log10() is available as a convenience.
This same formula is the foundation for solving exponential equations. When you have a * e^(b*x) = y and need to isolate x, taking Math.log() of both sides turns the exponent into a multiplication that you can rearrange with basic algebra.
Solving exponential equations
const solveExponential = (a, b, y) => {
// Solve for x: a * e^(b*x) = y
return Math.log(y / a) / b;
};
console.log(solveExponential(5, 0.1, 10));
// 6.9315...
Log returns are the standard way to measure relative change in finance and statistics. Unlike percentage returns, log returns are additive across time — the log return over two periods equals the sum of the individual log returns. This property makes them much easier to work with in models and regressions.
Measuring relative change
function logReturn(startPrice, endPrice) {
return Math.log(endPrice / startPrice);
}
console.log(logReturn(100, 110));
// 0.09531 (approximately 9.5% log return)
Log returns are commonly used in finance because they are additive across time periods, whereas percentage returns are multiplicative.
Logarithms also map ratios to a linear scale — converting a voltage ratio to decibels is a classic example. The same pattern applies to any domain where you want to express multiplicative differences as additive ones, from sound pressure to signal gain.
Logarithmic scale conversion
const toDecibels = (ratio) => 20 * Math.log10(ratio);
const fromDecibels = (db) => Math.pow(10, db / 20);
console.log(toDecibels(2));
// 6.0206 dB (approximately)
Entropy is another domain where logarithms are essential. The formula sums -p * log2(p) across all outcomes, measuring the average information content of a probability distribution. A fair coin flip produces exactly 1 bit of entropy because each outcome carries maximum uncertainty.
Computing entropy
function entropy(probabilities) {
return -probabilities.reduce((sum, p) => {
return p > 0 ? sum + p * Math.log2(p) : sum;
}, 0);
}
console.log(entropy([0.5, 0.5]));
// 1 bit (maximum uncertainty for 2 outcomes)
Precision near zero
When computing Math.log(1 + x) for very small values of x (close to 0), floating-point rounding can cause significant precision loss. The expression 1 + x discards the small increment because a 64-bit float cannot represent it accurately alongside the much larger 1. Use Math.log1p(x) instead, which is specifically designed to be accurate for small inputs:
// When x is very small, 1 + x loses the small x in floating point
const x = 1e-16;
console.log(Math.log(1 + x)); // 0 (incorrect — precision lost)
console.log(Math.log1p(x)); // 1e-16 (correct)
This matters in statistics and probability calculations where you frequently add small increments.
Convenience variants
JavaScript provides three specialised logarithm functions that are more accurate than applying the change-of-base formula manually:
Math.log2(x)— base-2 logarithm (useful for bit counting, information theory)Math.log10(x)— base-10 logarithm (useful for order-of-magnitude calculations)Math.log1p(x)— natural log of (1 + x), accurate for small x
For any other base, apply the change-of-base formula: Math.log(x) / Math.log(base).
See Also
- Math.log1p() — log(1 + x), more accurate for small x
- Math.log2() — base-2 logarithm
- Math.log10() — base-10 logarithm
- Math.exp() — exponential function (inverse)