CKTIS / JavaScript
Math.log10: base-10 logarithm
Math.log10 converts its argument to Number and returns an approximation of the base-10 logarithm.
Version: ECMAScript 2015 onward; example target: Node.js 24.22.2
Syntax
Math.log10(x)Math.log10(x) applies ToNumber and computes log₁₀(x). For positive values, log10(1) is +0, log10(10) is approximately 1, and log10(1000) approximately 3. Values between 0 and 1 produce a negative result, while +Infinity produces Infinity.
Both +0 and -0 produce -Infinity. A negative number, -Infinity, or NaN produces NaN; so does a missing argument. A numeric string is converted, a Symbol or BigInt throws TypeError, and an object can run conversion code. Approximation may be implementation-dependent.
The function is not a constructor and has been standard since ECMAScript 2015. It is useful for orders of magnitude, while Math.floor(Math.log10(n)) + 1 is only a quick estimate of a positive n’s decimal digit count. Because log10 is approximated, the estimate can be wrong near a power-of-10 boundary even for a safe integer. For a positive safe integer, use exact String(n).length; count BigInt digits from its decimal string too.
Examples
const values = [1, 10, 1000, 0.01];
console.log(values.map((value) => Math.log10(value)).join(","));
const n = 125000;
console.log(Math.floor(Math.log10(n)) + 1);
console.log(Math.log10(0), Math.log10(Infinity));
console.log(Number.isNaN(Math.log10(-1)), Number.isNaN(Math.log10()));
try { Math.log10(10n); } catch (error) { console.log(error instanceof TypeError); }Result
0,1,3,-2 6 -Infinity Infinity true true true