jsguides

Math.log10()

Math.log10(x)

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

ParameterTypeDescription
xnumberA number greater than or equal to zero

Return Value

InputReturn Value
x > 0The base-10 logarithm of x
x = 10 (since 10⁰ = 1)
x = 101 (since 10¹ = 10)
x = 1002 (since 10² = 100)
x = 0-Infinity
x < 0NaN
x = NaNNaN

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

The magnitude example gives you the power of ten a value sits at. A closely related computation is counting the digits in a number—the integer portion of the log10 result tells you how many powers of ten fit into the value, and adding one gives the total digit count. This pattern appears frequently in formatting, validation, and display logic.

// 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

The digit counting formula assumes a positive finite number. When the input is zero, negative, NaN, or Infinity, the arithmetic breaks down because those values sit outside the domain where log10 produces a meaningful finite result. Each edge case returns a distinct sentinel—checking the return value lets you handle them explicitly.

// 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

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)

These two functions serve different domains: Math.log() is the default for calculus and growth models, while Math.log10() is the better choice for decimal numbers, magnitudes, and any problem where the relevant scale is 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

The comparison above demonstrated the mathematical relationship between the two log functions. Now that the choice between them is clear, the next examples show practical applications where base-10 reasoning simplifies real-world code—starting with normalizing values across orders of magnitude.

// 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

The normalization example spreads values across a 0-to-1 range regardless of absolute scale. Another common pattern is extracting the exponent directly—you can read the order of magnitude from a number’s scientific notation by flooring the log10 of its absolute value.

// 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

Floating-point precision with exact powers of 10

Math.log10() returns exact integers for exact powers of 10 in most cases, but floating-point representation can introduce small errors. The examples above assume clean powers of ten, but in practice the binary representation of decimal fractions like 0.001 drifts a tiny amount, and Math.log10() reflects that imprecision.

Math.log10(1000);    // 3 (exact)
Math.log10(10000);   // 4 (exact)
Math.log10(100000);  // 5 (exact)

// But floating-point values can cause surprises:
Math.log10(0.1);     // -1 (exact)
Math.log10(0.001);   // -2.9999999999999996 (not exactly -3)

When you need an integer result, always apply Math.round() instead of Math.floor() to guard against these small rounding errors. The difference matters because floor and round disagree when the result is a shade above or below the true integer value—floor always truncates down, while round snaps to the nearest integer.

Math.round(Math.log10(0.001));  // -3 (correct)
Math.floor(Math.log10(0.001));  // -3 (correct here, but risky)
Math.floor(Math.log10(0.0001)); // may return -4 or -5 depending on precision

Using Math.log10() to determine decimal places needed

A practical application of Math.log10() is determining how many decimal places a number requires to represent it without scientific notation. Instead of extracting the integer exponent as the previous example did, this function inverts the sign and rounds up—perfect for deciding how many decimal digits to display in formatted output.

function decimalPlaces(n) {
  if (!Number.isFinite(n) || n === 0) return 0;
  const abs = Math.abs(n);
  if (abs >= 1) return 0;
  return Math.ceil(-Math.log10(abs));
}

decimalPlaces(0.1);    // 1
decimalPlaces(0.01);   // 2
decimalPlaces(0.001);  // 3
decimalPlaces(42);     // 0

Math.log10() and decibels

Decibels are a logarithmic scale based on powers of 10. Math.log10() is the core function for converting between raw amplitude ratios and dB values. The conversion uses a constant multiplier: 10 for power ratios and 20 for amplitude ratios, since power scales with the square of amplitude.

// Power ratio to decibels
function toDB(powerRatio) {
  return 10 * Math.log10(powerRatio);
}

// Amplitude ratio to decibels (audio)
function amplitudeToDb(amplitudeRatio) {
  return 20 * Math.log10(Math.abs(amplitudeRatio));
}

toDB(100);              // 20 dB
amplitudeToDb(0.5);     // -6.02 dB (halving amplitude)
amplitudeToDb(2);       // 6.02 dB (doubling amplitude)

The factor is 10 for power and 20 for amplitude (because power is proportional to amplitude squared).

When base-10 logarithms are the right tool

Base-10 logarithms are a natural fit any time your data is already expressed in decimal units. That includes digit counts, scientific notation, and many measurements that people read on a human scale instead of a binary one. If you are working with text formatting or presentation logic, Math.log10() often reads more clearly than a custom expression built from Math.log() and a conversion constant. It tells the next reader that the code is reasoning in powers of ten.

Avoiding off-by-one errors

Many developers write Math.floor(Math.log10(n)) + 1 for digit counts and then forget that the formula assumes n > 0. Zero, negative numbers, and floating-point values below 1 need separate handling. When the result feeds display logic, it is often better to branch first and only call Math.log10() for positive inputs. That keeps the edge cases visible and prevents small rounding differences from becoming user-facing bugs.

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