exponential
Math.log
Returns the natural logarithm (base e, or ln) of a number. Inverse of Math.exp.
Math.log(x) Parameters & edge cases
| Case | Behavior |
|---|---|
| x | A positive number; must be > 0 for a real result |
| log(0) | Math.log(0) returns -Infinity |
| log(negative) | Math.log(-1) returns NaN — no complex logs in JS |
Examples
console.log(Math.log(1)); // 0 ln(1) = 0 for any base.
console.log(Math.log(Math.E)); // 1 ln(e) = 1 — the defining property of the natural log.
console.log(Math.log(0)); // -Infinity Log of zero is negative infinity.
console.log(Math.log(-1)); // NaN Negative inputs return NaN.
Gotcha
Math.log is base-e, NOT base-10. Use Math.log10 or Math.log2 for common bases, or (Math.log(x) / Math.log(base)) for arbitrary bases.
Related
Math.log2
Returns the base-2 logarithm of a number. Introduced in ES2015 and useful for information theory, tree depths, and bit counts.
Math.log10
Returns the base-10 logarithm of a number. Introduced in ES2015 for accurate common-log calculations (decibels, pH, orders of magnitude).
Math.log1p
Returns the natural log of (1 + x), computed accurately even for very small x. Introduced in ES2015 as the companion to expm1.
Math.exp
Returns e^x, where e is Euler's number (~2.71828). The inverse of Math.log (the natural logarithm).
Math.expm1
Returns e^x - 1, computed accurately even for very small x. Introduced in ES2015 to avoid catastrophic cancellation near zero.