Javascript Reference - JavaScript log() Method








The log() method returns the natural logarithm (base E) of a number.

Browser Support

log() Yes Yes Yes Yes Yes

Syntax

Math.log(x);

Parameter Values

Parameter Description
x Required. A number




Return Value

A number representing the natural logarithm of a specified number.

If the parameter x is negative, NaN is returned.

If the parameter x is 0, -Infinity is returned.

Example

The following code shows how to calculate the natural logarithm (base E) of a number.

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
  function doMath() {<!--   ww  w. ja  v a  2  s  .co m-->
    var inputNum = document.form1.input.value;
    var result = Math.log(inputNum);
    document.form1.answer.value = result;
  }
</script>
</head>
<body>
  <form name=form1>
    This example calculates the natural log of the number entered. <br>
    <br> Enter First Number: <input type="text" name="input" size=15>
    <input type="button" value="Calculate" onClick='doMath()'> <br>
    The log is: <input type="text" name="answer" size=10>
  </form>
</body>
</html>

Click to view the demo