Math.log()
Math.log(x)
Returns the natural logarithm (base e) of a positive number.
Parameters
- x – a positive number.
Return value
A number; -Infinity for 0, NaN for a negative number.
Examples
console.log(Math.log(1), Math.log(Math.E), Math.log(0), Math.log(-1));
const logBase = (x, base) => Math.log(x) / Math.log(base);
console.log(logBase(8, 2), Math.log2(8));
console.log('How many times to double 1 to reach a million:', Math.ceil(Math.log2(1000000)));
Output:
0 1 -Infinity NaN 3 3 How many times to double 1 to reach a million: 20
Things to watch out for
- A logarithm with another base:
Math.log(x) / Math.log(base), for the bases 2 and 10 there are the more exactMath.log2()and Math.log10().
Browser support
Baseline – widely available: works in all major browsers and has for a long time (since 2015 at the latest). Since version: Chrome 1, Firefox 1, Safari 1; Node.js 0.10.0. As of October 2026 (MDN browser-compat-data).
Related:
Math.log10()
Math.pow()