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)); // 3Math.log(x)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)); // 0console.log(Math.log(0)); // -Infinity
console.log(Math.log(-1.355)); // NaN
console.log(Math.log(43 - 64)); // NaNconsole.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 4const x = 4;
console.log(Math.log(Math.exp(x))); // 4Math.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));
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.