exponential
Math.log10
Returns the base-10 logarithm of a number. Introduced in ES2015 for accurate common-log calculations (decibels, pH, orders of magnitude).
Math.log10(x) Parameters & edge cases
| Case | Behavior |
|---|---|
| x | A positive number |
| digit count | Math.floor(Math.log10(n)) + 1 gives the number of decimal digits in a positive integer |
| ES2015+ | Polyfill: Math.log(x) / Math.LN10 |
Examples
console.log(Math.log10(1000)); // 3 10³ = 1000.
console.log(Math.log10(1)); // 0 log₁₀(1) = 0.
console.log(Math.log10(100)); // 2 10² = 100.
console.log(Math.log10(0.1)); // -1 10⁻¹ = 0.1.
Gotcha
Math.log10(1000) === 3 in most engines but rounding errors can produce 2.9999999999999996 for other inputs — always round before using as an integer index.
Related
Math.log
Returns the natural logarithm (base e, or ln) of a number. Inverse of Math.exp.
Math.log2
Returns the base-2 logarithm of a number. Introduced in ES2015 and useful for information theory, tree depths, and bit counts.
Math.pow
Returns base raised to the power of exponent. In modern code the `**` operator (ES2016) is usually preferred for readability and identical semantics.
Math.exp
Returns e^x, where e is Euler's number (~2.71828). The inverse of Math.log (the natural logarithm).
Math.expm1
Returns e^x - 1, computed accurately even for very small x. Introduced in ES2015 to avoid catastrophic cancellation near zero.