JavaScript Math.log1p()
Syntax & Examples
Math.log1p() static-method
The Math.log1p() method in JavaScript returns the natural logarithm (㏒e, also known as ㏑) of 1 plus a given number (x). This is particularly useful for values of x close to zero, where using Math.log(1 + x) might result in a loss of precision. It is a static method of Math, meaning it is always used as Math.log1p() and not as a method of a Math object created (Math is not a constructor).
Syntax of Math.log1p()
The syntax of Math.log1p() static-method is:
Math.log1p(x)
This log1p() static-method of Math returns the natural logarithm (㏒e; also ㏑) of 1 + x for the number x.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
x | required | A number for which to return the natural logarithm of 1 plus the number. |
Return Type
Math.log1p() returns value of type Number
.
✐ Examples
1 Using Math.log1p() with a positive number
In this example, we use the Math.log1p()
method to find the natural logarithm of 1 plus 10.
For example,
- We call
Math.log1p()
with 10 as the argument. - The method returns the natural logarithm of 11.
- We log the result to the console using
console.log()
.
JavaScript Program
console.log(Math.log1p(10));
Output
2.3978952727983707
2 Using Math.log1p() with zero
In this example, we use the Math.log1p()
method to find the natural logarithm of 1 plus 0.
For example,
- We call
Math.log1p()
with 0 as the argument. - The method returns the natural logarithm of 1, which is 0.
- We log the result to the console using
console.log()
.
JavaScript Program
console.log(Math.log1p(0));
Output
0
3 Using Math.log1p() with a negative number
In this example, we use the Math.log1p()
method to find the natural logarithm of 1 plus -0.5.
For example,
- We call
Math.log1p()
with -0.5 as the argument. - The method returns the natural logarithm of 0.5.
- We log the result to the console using
console.log()
.
JavaScript Program
console.log(Math.log1p(-0.5));
Output
-0.6931471805599453
Summary
In this JavaScript tutorial, we learned about log1p() static-method of Math: the syntax and few working examples with output and detailed explanation for each example.