CKTIS / JavaScript
Math.log: natural logarithm
Math.log converts its argument to Number and returns an approximation of the natural logarithm with base e.
Version: ECMAScript 1 onward; example target: Node.js 24.22.2
Syntax
Math.log(x)Math.log(x) applies ToNumber and computes ln(x). For positive finite values the result can be negative, zero, or positive: log(1) is +0, log(Math.E) is approximately 1, and values between 0 and 1 produce a negative result. log(+Infinity) is Infinity.
Both +0 and -0 produce -Infinity, while a negative number, -Infinity, or NaN produces NaN. A missing argument also leads to NaN. A numeric string is converted, a Symbol or BigInt throws TypeError, and an object can invoke valueOf or toString. Approximation may be implementation-dependent.
The function is not a constructor and has been standard since ECMAScript 1. It helps recover an exponent, for example continuous rate Math.log(end / start) / time, but every quantity inside the logarithm must be positive. For ln(1+x) with very small x, use Math.log1p(x) to reduce loss of significant digits.
Examples
const start = 100;
const end = 150;
const years = 5;
const continuousRate = Math.log(end / start) / years;
console.log(continuousRate.toFixed(6), (continuousRate * 100).toFixed(3));
console.log(Math.log(1), Math.log(Math.E));
console.log(Math.log(0), Number.isNaN(Math.log(-1)));
console.log(Number.isNaN(Math.log()));
try { Math.log(1n); } catch (error) { console.log(error instanceof TypeError); }Result
0.081093 8.109 0 1 -Infinity true true true