# How to get the natural logarithm (base E) of a number in JavaScript?

JavascriptWeb DevelopmentFront End Technology

#### JavaScript for beginners

Best Seller

74 Lectures 10 hours

#### Modern Javascript for Beginners + Javascript Projects

Most Popular

112 Lectures 15 hours

#### The Complete Full-Stack JavaScript Course!

Best Seller

96 Lectures 24 hours

In this tutorial, we will learn how to get the natural logarithm of a number in JavaScript using Math.log() and Math.LN methods

Since finding out the natural logarithm of a number through calculation is difficult, the methods like Math.log() and Math.LN is considered useful in JavaScript. You can use these methods not only in finding the length of a number in JavaScript but also while creating a calculation method for a number.

## Using the Math.log() Method

The natural logarithm method is used to find a logarithm of a number with the base E. Compared to Math.LN method, while using Math.log() method in JavaScript, you are not limited to finding the output of certain numbers.

### Syntax

We can get the base E of a number by using Math.log() method. However, you won’t get the result for a negative number. The base E for 0 and 1 are -Infinity and 0. You can get base E for floating as well as numbers that are greater than 1.

Math.log(x);


### Example 1

In this example, we have used floating numbers 2.5 and 20.0 to get the base E with Math.log() method. You can experiment by using different outputs for any number greater than 1.

<html>
<body>
<h4>Using <i>Math.log()</i> method in JavaScript</h4>
<p id = "result1"> </p>
<p id = "result2"> </p>
<script>
var value1 = Math.log(2.5);
document.getElementById('result1').innerHTML = value1;
let value2 = Math.log(20.0);
document.getElementById('result2').innerHTML = value2;
</script>
</body>
</html>


Since now you know the basic method of using Math.log() in JavaScript, let’s understand how we can get the same output using the addition method. In this method, we will use multiplication logic. The numbers (x and y) can be multiplied to get another number (z). We can add Math.log(x) + Math.log(y) to get the same output as Math.log(z).

### Syntax

Using the following syntax, we will compare the base E of different numbers in JavaScript.

let x = Math.log(2) + Math.log(3);
let y = Math.log(6);


### Example 2

Here is the example where we have applied the addition and Math.log() method to compare the base E of different numbers. You can experiment with the numbers and find out different methods that can be used with Math.log().

<html>
<body>
<h2>JavaScript <i>Math log()</i> Method</h2>
<p id=log2></p>
<p id=log4></p>
<script>
var val1 = Math.log(2) + Math.log(2) + Math.log(2) + Math.log(2);
let val2 = Math.log(16);
document.getElementById('log2').innerHTML = val1 + "<br>Math.log(2) four times gives same output as Math.log(16)<br>" + val2;
let four = Math.log(4) + Math.log(3);
let val3 = Math.log(12);
document.getElementById('log4').innerHTML = four + "<br>Math.log(4) summed with Math.log(3) gives same output as Math.log(12)<br>" + val3;
</script>
</body>
</html>


## Using Math.LN2 and Math.LN10 method in JavaScript

Math.LN method is less commonly used compared to Math.log() because you are limited to using 2 and 10 as exponents. You can’t get the base E of most numbers only by using Math.LN methods. There might be a need to write a separate code.

### Syntax

Using the Math.LN2 in this syntax will give the same output as Math.log(2). However, you can consider using Math.LN method, if you are using 2 and 10 as exponents.

Var two = Math.LN2;


### Example

In the following example, we have used Math.LN method to get the base E of 2 and 10 numbers in JavaScript.

<html>
<body>
<h3>Using <i>Math.LN2</i> method in JavaScript</h3>
<p id="result1"></p>
<p id="result2"></p>
lt;script>
document.getElementById('result1').innerHTML = Math.LN2;
document.getElementById('result2').innerHTML = Math.LN10;
</script>
</body>
</html>


## Using Math.log() with Math.LN2 and Math.LN10

In this method, we will check how 2 and 10 as exponents can give similar base E of the number. Through the example, you can also understand Math.log() and Math.LN method can be used to get the same output.

### Syntax

Using this syntax, we will determine how the addition method can be used in place of the actual numbers to get its base E.

var two = Math.LN2 + Math.LN2;
var four = Math.log(4);


### Example

Here we have used Math.log(16) as the number to check the addition of the exponent Math.LN2 four times gives the same output. Since in the Math.LN method, we can’t use number 16 or 4; the result is generated using Math.LN2 in JavaScript.

<html>
<body>
<h4>Using <i>Math log()</i> with Math.LN2 and Math.LN10</h4>
<p id=ln2></p>
<p id=ln10></p>
<script>
var val1 = Math.LN2 + Math.LN2 + Math.LN2 + Math.LN2;
let val2 = Math.log(16);
document.getElementById('ln2').innerHTML = val1 + "<br>Math.LN2 four times gives same output as Math.log(16)<br>" + val2;
let ten = Math.LN10 + Math.LN10;
let val3 = Math.log(100);
document.getElementById('ln10').innerHTML = ten + "<br>Math.LN10 twice gives same output as Math.log(100)<br>" + val3;
</script>
</body>
</html>


Both Math.LN and Math.log() give the base E of a number and are used to create various scripts for the applications as well as online games. However, if you want to use random numbers that are greater than 1 to get its base E, then Math.log() method should be the preferred option. You can also create a function that allows you to get the natural logarithm of a number.

Updated on 14-Sep-2022 08:36:43