JavaScript Math.log10() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

Math.log10(x) returns the base-10 logarithm of a number. It tells you the power of 10 that gives x. For example, Math.log10(1000) is 3.

Math is built into JavaScript. Call the method directly. It is not a constructor, so never write new Math().

Syntax

Call Math.log10() like this. Start with this line.

JavaScript
Math.log10(x)
CallWhat it does
Math.log10(x)Returns the base-10 logarithm of x.

Built into JavaScript. Call Math.log10() on the Math object — no import required.

Library declarations
JavaScript
Math.log10(x)
JavaScript
interface Math {
  log10(x: number): number;
}

Parameters

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

Return Value

Return ValueDescription
numberThe base-10 logarithm of x. Returns -Infinity for 0, and NaN for negative numbers.

JavaScript Examples

Example 1 — Basic Use

Powers of ten give whole numbers.

JavaScript
console.log(Math.log10(1000));   // 3
console.log(Math.log10(100));    // 2
console.log(Math.log10(1));      // 0
console.log(Math.log10(0.01));   // -2
console.log(Math.log10(0));      // -Infinity
Try It Yourself

How It Works

  1. Powers of ten. 10 ** 3 is 1000, so Math.log10(1000) is 3.
  2. One. 10 ** 0 is 1, so the result is 0.
  3. Fractions. 0.01 is 10 ** -2, so the result is -2.
  4. Zero. There is no power that gives 0, so you get -Infinity.

Example 2 — Count the Digits

Math.log10() is a quick way to find how many digits a whole number has.

JavaScript
function countDigits(n) {
  return Math.floor(Math.log10(n)) + 1;
}

console.log(countDigits(9));      // 1
console.log(countDigits(10));     // 2
console.log(countDigits(12345));  // 5
Try It Yourself

How It Works

  1. log10. Math.log10(12345) is about 4.09.
  2. Floor. Math.floor(4.09) is 4.
  3. Add 1. A number with 4 as its floor has 5 digits.

Example 3 — pH and Decibels

Science often uses base-10 logs. Here are two small formulas.

JavaScript
function pH(hydrogen) {
  return -Math.log10(hydrogen);
}

function decibels(ratio) {
  return 10 * Math.log10(ratio);
}

console.log(pH(0.001));       // 3
console.log(decibels(1000));  // 30
console.log(decibels(100));   // 20
Try It Yourself

How It Works

  1. pH. The pH is the negative log10 of the hydrogen concentration. 0.001 gives pH 3.
  2. Decibels. Decibels are 10 * log10(ratio) of two power values.
  3. Examples. A ratio of 1000 is 30 dB and 100 is 20 dB.

Browser & Runtime Support

Math.log10() is part of ES2015 and is Baseline Widely available in modern browsers and Node.js.

✓ Baseline · ES2015

Math.log10()

Supported in Chrome, Firefox, Safari, Edge, and Node.js for years. Safe for modern apps without a polyfill.

98% 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.log10 Excellent

Bottom line: Use Math.log10() in new code. Only very old browsers, like Internet Explorer, lack it.

Frequently Asked Questions

Math.log10(x) returns the base-10 logarithm of x. Math.log10(1000) is 3 and Math.log10(100) is 2.
It returns -Infinity. Math.log10(-5) returns NaN.
Use Math.floor(Math.log10(n)) + 1 for positive whole numbers. For 12345 the answer is 5.
That can give tiny errors, like 2.9999999999999996 for 1000. Math.log10(1000) gives exactly 3.
It was added in ES2015 and works in all modern browsers and Node.js.

Next: log1p()

Learn how to find the natural log of 1 plus a number.

log1p() 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.

9 people found this page helpful