Math.log1p()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
TheMath.log1p() static method returns the natural logarithm (basee) of1 + x, wherex is the argument. That is:
In this article
Try it
console.log(Math.log1p(1));// Expected output: 0.6931471805599453console.log(Math.log1p(0));// Expected output: 0console.log(Math.log1p(-1));// Expected output: -Infinityconsole.log(Math.log1p(-2));// Expected output: NaNSyntax
Math.log1p(x)Parameters
xA number greater than or equal to -1.
Return value
The natural logarithm (basee) ofx + 1. Ifx is -1, returns-Infinity. Ifx < -1, returnsNaN.
Description
For very small values ofx, adding 1 can reduce or eliminate precision. The double floats used in JS give you about 15 digits of precision. 1 + 1e-15 = 1.000000000000001, but 1 + 1e-16 = 1.000000000000000 and therefore exactly 1.0 in that arithmetic, because digits past 15 are rounded off.
When you calculate log(1 +x), wherex is a small positive number, you should get an answer very close tox because:. If you calculateMath.log(1 + 1.1111111111e-15), you should get an answer close to1.1111111111e-15. Instead, you will end up taking the logarithm of1.00000000000000111022 (the roundoff is in binary, so sometimes it gets ugly), and get the answer 1.11022…e-15, with only 3 correct digits. If you calculateMath.log1p(1.1111111111e-15) instead, you will get a much more accurate answer,1.1111111110999995e-15, with 15 correct digits of precision (actually 16 in this case).
If the value ofx is less than -1, the return value is alwaysNaN.
Becauselog1p() is a static method ofMath, you always use it asMath.log1p(), rather than as a method of aMath object you created (Math is not a constructor).
Examples
>Using Math.log1p()
Math.log1p(-2); // NaNMath.log1p(-1); // -InfinityMath.log1p(-0); // -0Math.log1p(0); // 0Math.log1p(1); // 0.6931471805599453Math.log1p(Infinity); // InfinitySpecifications
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-math.log1p> |