JavaScript Math.log() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

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.

JavaScript
Math.log(x)
CallWhat 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
JavaScript
Math.log(x)
JavaScript
interface Math {
  log(x: number): number;
}

Parameters

ParameterDescription
xRequired. A number greater than 0. Zero returns -Infinity. Negative numbers return NaN.

Return Value

Return ValueDescription
numberThe 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.

JavaScript
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
Try It Yourself

How It Works

  1. One. e to the power 0 is 1, so Math.log(1) is 0.
  2. e. Math.log(Math.E) is 1.
  3. Zero. No power gives 0, so the result is -Infinity.
  4. Negative. Logs of negative numbers return NaN.

Example 2 — Log With Any Base

Divide two natural logs to get a log in another base.

JavaScript
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
Try It Yourself

How It Works

  1. The formula. log(x) / log(base) works for any base.
  2. Tiny errors. logBase(1000, 10) is 2.9999999999999996, not exactly 3.
  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?

JavaScript
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
Try It Yourself

How It Works

  1. Growth factor. Each year the money is multiplied by 1.07.
  2. Solve. We want 1.07 ^ years = 2, so years = log(2) / log(1.07).
  3. Result. It takes about 10.2 years.

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 support
Math.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.

Next: log10()

Learn how to find the base-10 logarithm.

log10() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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