JavaScript Math log() method

The log method in JavaScript's Math object computes the natural logarithm of a specified number, utilizing base e. If the argument provided is a negative number, the method will yield NaN.

Syntax

The syntax for the log function is expressed as follows:

Example

Math.log(num)

Parameter

num - A number.

Return

The natural logarithm of a number.

JavaScript Math log method example

In this section, we will explore the log method by examining a range of examples.

Example 1

Let’s consider an example that demonstrates how to output the natural logarithm of a given number.

Example

<script>

document.writeln(Math.log(1)+"<br>");

document.writeln(Math.log(5)+"<br>");

document.writeln(Math.log(10));

</script>

Output:

Output

0

1.6094379124341003

2.302585092994046

Example 2

Let us examine the outcomes of the log function across various test scenarios.

Example

<script>

document.writeln(Math.log()+"<br>");

document.writeln(Math.log(-5)+"<br>");

document.writeln(Math.log(0));

</script>

Output:

Output

NaN

NaN

-Infinity

Example 3

In this section, you have the opportunity to evaluate the log method using your personalized test scenarios.

Example

<script>

function display()

{

  var x=document.getElementById("num").value;

  document.getElementById("result").innerHTML=Math.log(x);

}

</script>

<form>

  Enter a number: <input type="text" id="num">

  <input type="button"  onclick="display()" value="submit">

  </form>

  <p><span id="result"></span></p>

Input Required

This code uses input(). Please provide values below: