JavaScript Math.log()

Math.log(x) returns the natural logarithm of x, using Euler's number e as the base. For base 10 and base 2, JavaScript also provides Math.log10() and Math.log2().

console.log(Math.log(Math.E)); // 1
console.log(Math.log10(1000)); // 3

Math.log() syntax Top ↑

Math.log(x)

Natural-log examples Top ↑

console.log(Math.log(2));   // 0.6931471805599453
console.log(Math.log(21));  // 3.044522437723423
console.log(Math.log(2.5)); // 0.9162907318741551
console.log(Math.log(1));   // 0

Zero and negative values Top ↑

console.log(Math.log(0));    // -Infinity
console.log(Math.log(-1.355)); // NaN
console.log(Math.log(43 - 64)); // NaN

Base 10, base 2 and custom logarithms Top ↑

console.log(Math.log10(1000)); // 3
console.log(Math.log2(8));     // 3

function logBase(value, base) {
  return Math.log(value) / Math.log(base);
}
console.log(logBase(625, 5)); // approximately 4

Math.log() and Math.exp() Top ↑

const x = 4;
console.log(Math.log(Math.exp(x))); // 4

For values close to 1, consider Math.log1p() Top ↑

Math.log1p(x) calculates ln(1 + x) and can be more accurate for very small values of x than calculating Math.log(1 + x) directly.

const tiny = 1e-12;
console.log(Math.log1p(tiny));

Math.exp()




Subscribe to our YouTube Channel here



plus2net.com










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer