JavaScript Math.log() Method

Definition
Math.log(x) returns the natural logarithm of a number. That is the power you must raise e (about 2.71828) to, to get x. It is often written ln.
Math is built into JavaScript. Call the method directly. It is not a constructor, so never write new Math().
Syntax
Call Math.log() like this. Start with this line.
| Call | What it does |
|---|
Math.log(x) | Returns the natural logarithm (base e) of x. |
Built into JavaScript. Call Math.log() on the Math object — no import required.
Library declarations
interface Math {
log(x: number): number;
}
Parameters
| Parameter | Description |
|---|
| x | Required. A number greater than 0. Zero returns -Infinity. Negative numbers return NaN. |
Return Value
| Return Value | Description |
|---|
| number | The natural logarithm of x. Returns -Infinity for 0, and NaN for negative numbers. |
JavaScript Examples
Example 1 — Basic Use
The result is the power of e that gives your number.
console.log(Math.log(1)); // 0
console.log(Math.log(Math.E)); // 1
console.log(Math.log(10)); // 2.302585092994046
console.log(Math.log(0)); // -Infinity
console.log(Math.log(-1)); // NaN
0
1
2.302585092994046
-Infinity
NaN
How It Works
- One.
e to the power 0 is 1, so Math.log(1) is 0. - e.
Math.log(Math.E) is 1. - Zero. No power gives
0, so the result is -Infinity. - Negative. Logs of negative numbers return
NaN.
Example 2 — Log With Any Base
Divide two natural logs to get a log in another base.
function logBase(x, base) {
return Math.log(x) / Math.log(base);
}
console.log(logBase(8, 2)); // 3
console.log(logBase(1000, 10)); // 2.9999999999999996
console.log(Math.round(logBase(1000, 10))); // 3
console.log(Math.log10(1000)); // 3
How It Works
- The formula.
log(x) / log(base) works for any base. - Tiny errors.
logBase(1000, 10) is 2.9999999999999996, not exactly 3. - Fix. Round the answer, or use
Math.log10() for base 10.
Example 3 — How Long to Double?
At a growth rate of 7% per year, how many years until money doubles?
var rate = 0.07;
var years = Math.log(2) / Math.log(1 + rate);
console.log(years); // 10.244768351058712
console.log(years.toFixed(1) + " years"); // 10.2 years
10.244768351058712
10.2 years
How It Works
- Growth factor. Each year the money is multiplied by
1.07. - Solve. We want
1.07 ^ years = 2, so years = log(2) / log(1.07). - Result. It takes about 10.2 years.
Compatibility
Browser & Runtime Support
Math.log() has been part of JavaScript since ES1 and is Baseline Widely available everywhere.
✓ Baseline · ES1
Math.log()
Supported in every browser and Node.js version. No polyfill is needed.
99% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full supportMath.log Excellent
Bottom line: Use Math.log() freely. It works in all browsers and runtimes.
Frequently Asked Questions
Math.log(x) returns the natural logarithm (base e) of x. Math.log(1) is 0 and Math.log(Math.E) is 1.
No. It is base e. For base 10 use Math.log10(), and for base 2 use Math.log2().
It returns -Infinity. Math.log(-1) returns NaN, because logs of negative numbers are not real numbers.
Divide two natural logs: Math.log(x) / Math.log(base). For example, Math.log(8) / Math.log(2) is 3.
Math.exp(). Math.exp(Math.log(x)) gives back x.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful