Math.log10()
Math.log10(x) Returns:
number · Added in vES1 · Updated March 13, 2026 · Math math logarithm base-10 numbers scientific data-science
Math.log10() returns the base-10 logarithm of a number. It’s the go-to function when you need to work with decimal (base-10) systems, making it indispensable for calculating magnitude, digit counts, pH calculations, decibel conversions, and any application involving powers of 10.
Syntax
Math.log10(x)
Parameters
| Parameter | Type | Description |
|---|---|---|
x | number | A number greater than or equal to zero |
Return Value
| Input | Return Value |
|---|---|
x > 0 | The base-10 logarithm of x |
x = 1 | 0 (since 10⁰ = 1) |
x = 10 | 1 (since 10¹ = 10) |
x = 100 | 2 (since 10² = 100) |
x = 0 | -Infinity |
x < 0 | NaN |
x = NaN | NaN |
Examples
Calculating the magnitude of a number
// Determine how many powers of 10: the "order of magnitude"
console.log(Math.log10(1));
// 0
console.log(Math.log10(50));
// 1.6989700043360187
console.log(Math.log10(1000));
// 3
console.log(Math.log10(0.001));
// -3
Counting digits in a number
// Useful for formatting, validation, and display
function countDigits(n) {
return Math.floor(Math.log10(n)) + 1;
}
console.log(countDigits(12345));
// 5
console.log(countDigits(999));
// 3
console.log(countDigits(1));
// 1
Handling edge cases
// Zero returns -Infinity
console.log(Math.log10(0));
// -Infinity
// Negative numbers return NaN (no real logarithm exists)
console.log(Math.log10(-5));
// NaN
// NaN propagates
console.log(Math.log10(NaN));
// NaN
// Infinity returns Infinity
console.log(Math.log10(Infinity));
// Infinity
Comparison: Math.log10() vs Math.log()
While both functions calculate logarithms, they differ in their base:
Math.log(x)— natural logarithm (base e ≈ 2.718)Math.log10(x)— common logarithm (base 10)
// Math.log10 uses base 10
console.log(Math.log10(100));
// 2 (because 10² = 100)
// Math.log uses base e
console.log(Math.log(100));
// 4.605170185988092 (because e^4.605... = 100)
// You can derive Math.log10 using Math.log and Math.LN10
console.log(Math.log(100) / Math.LN10);
// 2 (same as Math.log10(100))
// Math.LN10 is a constant: Math.log(10) ≈ 2.302585
console.log(Math.LN10);
// 2.302585093
Common Use Cases
Data science and scaling
// Normalize values using log10 to handle exponential data
function normalizeLogScale(values) {
const max = Math.max(...values);
return values.map(v => Math.log10(v) / Math.log10(max));
}
console.log(normalizeLogScale([1, 10, 100, 1000]));
// [0, 0.333..., 0.666..., 1]
Scientific notation conversion
// Extract the exponent from scientific notation
function getScientificExponent(n) {
return Math.floor(Math.log10(Math.abs(n)));
}
console.log(getScientificExponent(3.14e5));
// 5
console.log(getScientificExponent(0.00042));
// -4
Performance Note
Math.log10() is a native JavaScript built-in, optimized in modern engines. For bulk computations in tight loops, the performance is generally adequate. If you need to convert many numbers and know the base is fixed, pre-computing 1 / Math.LN10 can occasionally save a division, but the difference is negligible in most applications.
See Also
- math::log2 — base-2 logarithm
- math::LN10 — the natural logarithm of 10
- math::exp — exponential function
- math::pow — exponentiation