JavaScript Math.log(), Math.log10(), and Math.log2() Compared

In JavaScript, Math.log(x) is the natural logarithm, Math.log10(x) uses base 10, and Math.log2(x) uses base 2. For another base b, calculate Math.log(x) / Math.log(b).

Choose the method for base e, 10, or 2

A logarithm is the exponent you apply to the base to obtain a given number. Math.log10(100) is 2 and Math.log2(16) is 4. The natural base e is available as Math.E. ECMAScript · Math.log() Math.log10() Math.log2()

console.log(Math.log(Math.E)); // 1
console.log(Math.log10(100)); // 2
console.log(Math.log2(16)); // 4

Check the domain and calculate with another base

For a finite real logarithm, x must be positive and finite. The base must also be positive and finite and differ from 1. Math.log(0) returns -Infinity and negative numbers return NaN. A base between 0 and 1 is valid. For example, the logarithm of 4 in base 0.5 is -2.

console.log(Math.log(0)); // -Infinity
console.log(Math.log(-1)); // NaN
console.log(Math.log(64) / Math.log(4)); // ≈ 3
console.log(Math.log(81) / Math.log(3)); // ≈ 4
console.log(Math.log(4) / Math.log(0.5)); // ≈ -2

const tiny = 1e-16;
console.log(Math.log(1 + tiny)); // 0
console.log(Math.log1p(tiny));    // ≈ 1e-16

Logarithms use floating-point approximations. To compute ln(1 + x) for a tiny increment x, use Math.log1p(x). With x = 1e-16, JavaScript rounds 1 + x to 1, so the plain logarithm returns 0 while log1p() preserves a nonzero approximation. ECMAScript · Math.log1p()

Compare logarithms in a complete example

Save the code as math-log.html in UTF-8 and open it in a browser. Enter the value and a base to see the three built-in logarithms and the result after changing base. Only the display is rounded to 12 significant digits.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Compare logarithm functions</title>
</head>
<body>
<label for="number">Value</label>
<input id="number" type="number" step="any" value="64">
<label for="base">Additional base</label>
<input id="base" type="number" step="any" value="4">
<button id="calculate" type="button">Calculate</button>
<pre id="result" aria-live="polite"></pre>
<script>
const input = document.querySelector('#number');
const baseInput = document.querySelector('#base');
const result = document.querySelector('#result');

function show() {
  const value = input.valueAsNumber;
  const base = baseInput.valueAsNumber;
  if (!Number.isFinite(value) || !Number.isFinite(base) ||
      value <= 0 || base <= 0 || base === 1) {
    result.textContent = "Enter a positive finite value and a positive finite base other than 1.";
    return;
  }
  const format = number => Number(number.toPrecision(12));
  result.textContent = [
    `Natural log: ${format(Math.log(value))}`,
    `Base 10: ${format(Math.log10(value))}`,
    `Base 2: ${format(Math.log2(value))}`,
    `Base ${base}: ${format(Math.log(value) / Math.log(base))}`
  ].join('\n');
}

document.querySelector('#calculate').addEventListener('click', show);
show();
</script>
</body>
</html>

The default value 64 and base 4 show 3 on the final line. Try 81 and base 3 for 4, or 4 and base 0.5 for -2. A zero or negative value or a base of 1 shows an input message.