totn JavaScript

JavaScript: Math log1p() function

This JavaScript tutorial explains how to use the math function called log1p() with syntax and examples.

Description

In JavaScript, log1p() is a function that is used to return the natural logarithm of a number plus 1 which is loge(number+1). Because the log1p() function is a static function of the Math object, it must be invoked through the placeholder object called Math.

Syntax

In JavaScript, the syntax for the log1p() function is:

Math.log1p(number);

Parameters or Arguments

number
The number to use in the calculation. It must be a value that is greater than -1.

Returns

The log1p() function returns the natural logarithm of a number after first adding 1 to the number.

If the number is -1, the log1p() function will return -Infinity.

If the number is less than -1, the log1p() function will return NaN.

Note

  • Math is a placeholder object that contains mathematical functions and constants of which log1p() is one of these functions.

Example

Let's take a look at an example of how to use the log1p() function in JavaScript.

For example:

console.log(Math.log1p(1));
console.log(Math.log1p(2.5));
console.log(Math.log1p(-1));
console.log(Math.log1p(-1.1));

In this example, we have invoked the log1p() function using the Math class.

We have written the output of the log1p() function to the web browser console log, for demonstration purposes, to show what the log1p() function returns.

The following will be output to the web browser console log:

0.6931471805599453
1.252762968495368
-Infinity
NaN

In this example, the first output to the console log returned 0.6931471805599453 which is the natural logarithm of 2 (calculation: 1+1=2 so it returned the natural logarithm of 2).

The second output to the console log returned 1.252762968495368 which is the natural logarithm of 3.5 (calculation: 2.5+1=3.5 so it returned the natural logarithm of 3.5).

The second output to the console log returned -Infinity since the number provided was -1 (calculation: -1+1=0 so it returned the natural logarithm of 0).

The second output to the console log returned NaN since the number provided was 0 or less (calculation: 0+1=1 so it returned the natural logarithm of 1).