How can I specify the base for Math.log() in JavaScript?


In this tutorial, we will learn to calculate the logarithm of any number value and specify the base for the Math.log() method.

The logarithmic operation of mathematics is beneficial in many places. For example, if you want to show the graph representation for billions of values, it is hard to render on the screen. For that, we change the scale of the graph using the logarithm, and it becomes easy to generate and visualize the output for any particular operation.

There can be many more examples where we can use the logarithmic function.

  • Calculate the log using the Math.log()

  • Specify different bases for Math.log()

  • Specify base 10 using the Math.log10()

Calculate the log using the Math.log()

In JavaScript, Math.log() is the built-in method of the Math library to calculate the natural logarithm of the different number values. It takes a number value as a parameter and returns the logarithm of the number with base E, which means natural logarithm. In the mathematics language, we can say that Math.log() returns the value of ln( number ).

Syntax

Here is the syntax to use the Math.log() function in JavaScript.

let output = Math.log( number )

Parameters

  • number − It is a value for which we want to calculate the natural logarithm.

Return values

  • The Math.log() function returns the natural logarithm of positive numeric values. For the 0, it returns the -Infinity, and for negative and non-numeric values, it returns NaN.

  • For the string numeric input, the function parse the numeric value automatically from the string and returns the output.

Example

The below example demonstrates the use of the Math.log() method with different input values.

<!DOCTYPE html>
<html>
<head>
   <title> Calculating the log using the Math.log(). </title>
</head>
<body>
   <h2> Calculate the natural logarithm using the Math.log() function </h2>
   <h4> Natural logarithm of 10000 </h4>
   <div id="output1"> </div>
   <h4> Natural logarithm of -165 </h4>
   <div id="output2"> </div>
   <h4> Natural logarithm of "223" </h4>
   <div id="output3"> </div>
   <h4> natural logarithm of "2ws" </h4>
   <div id="output4"> </div>
   <script type="text/javascript">
      let output1 = document.getElementById("output1");
      let output2 = document.getElementById("output2");
      let output3 = document.getElementById("output3");
      let output4 = document.getElementById("output4");
      var number = 10000;
      output1.innerHTML = Math.log(number);
      number = -165;
      output2.innerHTML = Math.log(number);
      number = "223"; // function automatically parses the number from string
      output3.innerHTML = Math.log(number);
      number = "2ws";
      output4.innerHTML = Math.log(number);
   </script>
</body>
</html>

In the above output, users can see that for the negative and non-numeric values, Math.log() function returns NaN.

Specify different bases for Math.log()

In this section, we will learn to specify the different bases for the logarithmic functions. There is no built-in functionality to change the base of the logarithm in JavaScript, but we can apply some mathematics logic to change the base.

In mathematics,

$$\mathrm{\mathit{log_{b}}\left ( \mathit{X} \right )=\:\mathit{log_{nb}\left ( X \right )}/\mathit{log_{nb}\left ( b \right )}}$$

Using the above way, we can change the base for the logarithm in mathematics. The above formula suggests that if users want to change the base for the logarithm, they can divide the logarithm value by the logarithm of the base by setting up a new base (nb) for both. In our case, we will set a natural logarithmic base (e) for both values.

Syntax

Users can follow the below syntax to change the base.

let output = Math.log( number ) / Math.log( base );

Example

The below example, demonstrates to use of different base for the logarithmic function in JavaScript.

<!DOCTYPE html>
<html>
<head>
   <title> Calculating the log using the Math.log(). </title>
</head>
<body>
   <h2> Calculate the logarithm of numbers with different bases </h2>
   <h4> Logarithm of 4000 with respect to base 40. </h4>
   <div id="output1"> </div>
   <h4> Logarithm of 4000 with respect to base 20. </h4>
   <div id="output2"> </div>
   <script type="text/javascript">
      let output1 = document.getElementById("output1");
      let output2 = document.getElementById("output2");
      var number = 4000;
      output1.innerHTML = Math.log(number) / Math.log(40);
      output2.innerHTML = Math.log(number) / Math.log(20);
   </script>
</body>
</html>

Specify base 10 using the Math.log10() Method

The Math.log10() method is the same as the Math.log() method. The difference between both methods is that Math.log10() method uses the base 10, and Math.log() method uses natural logarithm (e) as the base.

Syntax

Users can follow the below syntax to use the Math.log10() method.

Let output = Math.log10( number );

Example

Users can learn how we can use Math.log10() method from the below example.

<!DOCTYPE html>
<html>
<head>
   <title> Calculating the log using the Math.log10(). </title>
</head>
<body>
   <h2> Calculate the logarithm of numbers using the Math.log10() Method</h2>
   <h4> Logarithm of 100000 with base 10 </h4>
   <div id="output1"> </div>
   <h4> Logarithm of 500000 with respect to base 10. </h4>
   <div id="output2"> </div>
   <script>
      let output1 = document.getElementById("output1");
      let output2 = document.getElementById("output2");
      var number = 100000;
      output1.innerHTML = Math.log10(number);
      number = 500000;
      output2.innerHTML = Math.log10(number);
   </script>
</body>
</html>

Conclusion

This tutorial taught us to change the base for the Math.log() function. If users want to change the base of the log to 10, they should use the third approach. For different bases, users can use the second approach.

Updated on: 14-Jul-2022

526 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements