JavaScript Math.log10() Method

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.
| Call | What 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
interface Math {
log10(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 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.
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
How It Works
- Powers of ten.
10 ** 3 is 1000, so Math.log10(1000) is 3. - One.
10 ** 0 is 1, so the result is 0. - Fractions.
0.01 is 10 ** -2, so the result is -2. - 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.
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
How It Works
- log10.
Math.log10(12345) is about 4.09. - Floor.
Math.floor(4.09) is 4. - 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.
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
How It Works
- pH. The pH is the negative
log10 of the hydrogen concentration. 0.001 gives pH 3. - Decibels. Decibels are
10 * log10(ratio) of two power values. - Examples. A ratio of
1000 is 30 dB and 100 is 20 dB.
Compatibility
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 supportMath.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.
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.
9 people found this page helpful