Math.log10()
Returns the base 10 logarithm of a number — the count of its digits or its order of magnitude.
Math.log10(x)- Added in JavaScript ES2015
Parameters
| Parameter | Type | Description |
|---|---|---|
$x | number | A number greater than or equal to 0. |
Return value
numberThe base 10 logarithm of x. Returns -Infinity if x is ±0, NaN if x < 0, and Infinity if x is Infinity.
Math.log10(x) returns the power of 10 that produces x. This is the log key on a pocket calculator — the one people reach for when they type Math.log() by mistake.
When to reach for it
Order of magnitude is the recurring use: Math.floor(Math.log10(n)) + 1 counts the digits of a positive integer, which is how you size column widths, pad line numbers, or pick a human-readable unit. Decibels are 20 * Math.log10(ratio) for amplitude and 10 * Math.log10(ratio) for power. pH, Richter magnitudes, and logarithmic chart axes are all base 10 by definition.
What trips people up
Floating point ruins the digit-count trick at the boundaries. Math.log10() of an exact power of ten is reliable in current engines, but once the input is itself a computed float, the result can land just below the integer and Math.floor() drops a digit. For integers under Number.MAX_SAFE_INTEGER, String(n).length is both simpler and exact.
Zero returns -Infinity and negatives return NaN, neither of which throws — a signal level of 0 quietly becomes -Infinity dB rather than raising an error.
Example
When to use it
- Count the digits in an integer to size a padded output column
- Convert an amplitude or power ratio to decibels
- Pick a tick spacing or unit prefix for a logarithmic axis
- Extract the exponent of a value in scientific notation
Frequently asked questions
What is the difference between Math.log() and Math.log10()?
Math.log() is the natural logarithm, base e, while Math.log10() is base 10. Math.log(100) returns 4.605 and Math.log10(100) returns 2 — if you meant the calculator's log key, you want Math.log10().
How do I count the number of digits in a number?
For a positive integer, Math.floor(Math.log10(n)) + 1 gives the digit count, but String(n).length is exact and simpler for any safe integer. Use the logarithm form only when you need a continuous order of magnitude rather than an integer count.
Why does Math.log10() give -Infinity in my decibel calculation?
The input was 0, and no power of 10 produces 0, so the logarithm diverges to -Infinity. Clamp the amplitude to a small floor such as 1e-6 before converting, which maps silence to a finite dB value.
More examples
Decibels with a safe floor
The raw call on a silent sample gives -Infinity, which breaks any later averaging or rendering. Clamping to a floor turns silence into a finite -120.0 dB, and taking the absolute value first keeps negative samples from becoming NaN.
const FLOOR = 1e-6; // -120 dB
const toDb = (amp) => 20 * Math.log10(Math.max(Math.abs(amp), FLOOR));
console.log(20 * Math.log10(0)); // -Infinity: unusable
console.log(toDb(0).toFixed(1));
console.log(toDb(1), toDb(0.5).toFixed(2));
console.log(toDb(-0.5).toFixed(2)); // sign ignoredDigit counting, and when to skip it
Both approaches agree across the powers-of-ten boundaries in current engines. The string version is exact by construction for safe integers, so prefer it for digit counts and keep Math.log10() for continuous magnitudes.
const byLog = (n) => Math.floor(Math.log10(n)) + 1;
const byString = (n) => String(n).length;
for (const n of [9, 10, 99, 100, 999999]) {
console.log(n, byLog(n), byString(n));
}Choosing a unit prefix
Dividing the base-10 log by 3 picks the SI tier directly. The zero case must be special-cased because Math.log10(0) is -Infinity, and the clamp keeps values beyond terabytes from indexing past the end of the array.
const UNITS = ["B", "kB", "MB", "GB", "TB"];
function humanSize(bytes) {
if (bytes === 0) return "0 B";
const tier = Math.min(Math.floor(Math.log10(bytes) / 3), UNITS.length - 1);
return (bytes / 10 ** (tier * 3)).toFixed(1) + " " + UNITS[tier];
}
console.log(humanSize(0), humanSize(512), humanSize(1500));
console.log(humanSize(2_400_000), humanSize(9e15));
Discussion