JavaScript Math log1p()

The JavaScript Math.log1p() function returns the natural logarithm of 1 plus the given number.

It is equivalent to ln(1+p) in mathematics.

The syntax of the Math.log1p() function is:

Math.log1p(x)

log1p(), being a static method, is called using the Math class name.


Math.log1p() Parameters

The Math.log1p() function takes in:

  • x - A number

Return value from Math.log1p()

  • Returns the natural logarithm (base e) of (1+ given number).
  • Returns NaN for number < -1 and non-numeric arguments.

Example: Using Math.log1p()

// Math.log1p() is equivalent to ln(1+x)

var value = Math.log1p(0); // ln(1)
console.log(value); // 0

var value = Math.log1p(1); // ln(2)
console.log(value); // 0.6931471805599453

var value = Math.log1p("5"); // ln(6)
console.log(value); // 1.791759469228055

var value = Math.log1p(-1); // ln(0)
console.log(value); // -Infinity

var value = Math.log1p(-2); // ln(-1)
console.log(value); // NaN

Output

0
0.6931471805599453
1.791759469228055
-Infinity
NaN

Recommended readings: