-1), it returns NaN as">

JavaScript Math.log1p() Method



The JavaScript Math.log1p() method accepts numeric value (x) as an argument and calculates the natural logarithm (base e) of 1 plus the provided number (x). If the provided number (x) is "-1", this method returns -Infinity as result. If the argument is less than "-1" (i.e. x > -1), it returns NaN as result.

Here's the mathematical representation of the Math.log1p() function −

log1p(x) = log(1 + x)

This method takes a single argument "x" and returns the natural logarithm of (1 + x). The returned value is a numeric value.

Syntax

Following is the syntax of JavaScript Math.log1p() method −

Math.log1p(x)

Parameters

This method accepts only one parameter. The same is described below −

  • x: A numeric expression.

Return value

This method returns the natural logarithm of 1 + the provided number (x).

Example 1

In the following example, we are using the JavaScript Math.log1p() method to calculate the natural logarithm (base e) of 1 + "2"

<html>
<body>
<script>
   const result = Math.log1p(2);
   document.write(result);
</script>
</body>
</html>

Output

After executing the above program, it returns approximately "1.0986" as result.

Example 2

Here, we are computing the base e log value of 1 + 0 −

<html>
<body>
<script>
   const result = Math.log1p(0);
   document.write(result);
</script>
</body>
</html>

Output

If we execute the program, it will return 0 as result.

Example 3

If we provide "-1" as an argument to this method, it returns -Infinity as result −

<html>
<body>
<script>
   const result = Math.log1p(-1);
   document.write(result);
</script>
</body>
</html>

Output

If we execute the program, it will return -Infinity as result.

Example 4

In this example, we are computing the base e log value of a negative number "-2" −

<html>
<body>
<script>
   const result = Math.log1p(-2);
   document.write(result);
</script>
</body>
</html>

Output

It returns NaN (not a number) as result.

Advertisements