Math.log()
Returns the natural logarithm (base e) of a number, the inverse of Math.exp().
Math.log(x)- Added in JavaScript ES1
Parameters
| Parameter | Type | Description |
|---|---|---|
$x | number | A number greater than or equal to 0. |
Return value
numberThe natural logarithm (base e) of x. Returns -Infinity if x is ±0, NaN if x < 0, and Infinity if x is Infinity.
Math.log(x) answers: to what power must e be raised to get x? It is the ln of a calculator, not the log button — that one is Math.log10(), and the mismatch is the single most common bug on this function.
When to reach for it
Natural log is the language of continuous rates. Converting a doubling time or a half-life into a decay constant, recovering a growth rate from a ratio, computing log-likelihoods, or compressing a long-tailed distribution before charting it all use Math.log(). It is also the general-purpose base changer: Math.log(x) / Math.log(b) gives log base b for any b.
What trips people up
Negative input returns NaN rather than throwing, so a bad value flows silently into the rest of your arithmetic — guard the input. Zero returns -Infinity, which propagates just as quietly through sums.
The base-change trick is not exact. Math.log(1000) / Math.log(10) is 2.9999999999999996, so never compare its result with === or feed it straight to an array index. Prefer the dedicated Math.log2() and Math.log10() when the base is 2 or 10, and round when you need an integer.
For x near 1 the answer is near 0 and precision collapses; Math.log1p(x) computes Math.log(1 + x) accurately instead.
Example
When to use it
- Turn a half-life or doubling time into a continuous rate constant
- Compute log base b of a value as Math.log(x) / Math.log(b)
- Apply a log transform to skewed data before plotting
- Accumulate log-probabilities instead of multiplying tiny floats
Frequently asked questions
Is Math.log() base 10 or base e?
Math.log() is base e, the natural logarithm — Math.log(100) returns 4.605, not 2. For the base-10 logarithm use Math.log10(), and for base 2 use Math.log2().
How do I take a logarithm with an arbitrary base in JavaScript?
Divide two natural logs: Math.log(x) / Math.log(base). There is no built-in for arbitrary bases, and the division introduces rounding error, so round the result when you expect a whole number.
Why does Math.log() return NaN instead of throwing?
Math.log() returns NaN for negative arguments and -Infinity for 0 because the IEEE 754 rules that govern JavaScript numbers define those results rather than raising errors. Validate the input yourself if a bad value should stop the computation.
More examples
Edge cases and the NaN trap
Bad input never throws: zero degrades to -Infinity and negatives to NaN, both of which silently contaminate any later sum or average. The guard turns those cases into an explicit null you can branch on.
console.log(Math.log(0)); // -Infinity
console.log(Math.log(-0)); // -Infinity
console.log(Math.log(-1)); // NaN, no exception
console.log(Math.log(Infinity));
const safeLog = (x) => (x > 0 ? Math.log(x) : null);
console.log([4, 0, -2].map(safeLog));Base conversion is not exact
The divided form gives 2.9999999999999996 and fails the strict equality, while Math.log10(1000) returns exactly 3. Base 2 happens to come out clean here, but that is luck, not a guarantee — round the result whenever you need an integer.
console.log(Math.log(1000) / Math.log(10));
console.log(Math.log10(1000));
console.log(Math.log(1000) / Math.log(10) === 3);
console.log(Math.log(8) / Math.log(2));
console.log(Math.log2(8));Precision near 1
Adding a tiny rate to 1 rounds off its low digits before the logarithm ever runs, so Math.log(1 + 1e-12) drifts from the accurate Math.log1p value and at 1e-17 collapses to exactly 0. Math.log1p() keeps full precision.
const rate = 1e-12; // a very small growth rate
console.log(Math.log(1 + rate));
console.log(Math.log1p(rate));
console.log(Math.log(1 + 1e-17)); // rounded away to 0
console.log(Math.log1p(1e-17));
Discussion