Base de connaissancesCentre d’examen
FRFrançais
Choisir la langue13

CKTIS / JavaScript

Math.log10 : logarithme décimal

Math.log10 convertit son argument en Number et renvoie une approximation du logarithme de base 10.

Version : ECMAScript 2015 onward; example target: Node.js 24.22.2

Syntaxe

Math.log10(x)

Math.log10(x) applique ToNumber et calcule log₁₀(x). log10(1) vaut +0, log10(10) environ 1 et log10(1000) environ 3. Une valeur entre 0 et 1 donne un résultat négatif et +Infinity donne Infinity.

±0 donne -Infinity. Un nombre négatif, -Infinity ou NaN donne NaN ; l’absence d’argument aussi. Une chaîne numérique est convertie, Symbol ou BigInt lève TypeError et un objet peut exécuter sa conversion. L’approximation dépend parfois de l’implémentation.

Pas un constructeur, standard depuis ECMAScript 2015. Utile pour les ordres de grandeur, mais Math.floor(Math.log10(n)) + 1 n’est qu’une estimation rapide des chiffres d’un n positif. L’approximation peut rendre cette estimation fausse près d’une puissance de 10, même pour un entier sûr. Pour un entier sûr positif, String(n).length est exact ; comptez aussi les chiffres de BigInt dans sa chaîne décimale.

Exemples

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); }

Résultat

0,1,3,-2
6
-Infinity Infinity
true true
true

Sources