Categories
JavaScript Web development

JavaScript Math.log()

JavaScript Math.log()

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

Syntax:

Math.log(x)

Note: A Number, representing the natural logarithm of a defined number. If the number is negative, NaN is returned.
If the number is 0, -Infinity is returned.

Example:

<!DOCTYPE html>
<html>
<body>

<button onclick="logFunction()">Try it</button>

<p id="example"></p>

<script>
function logFunction() {
  var a = Math.log2(2);
  var b = Math.log2(1);
  var c = Math.log2(0);
  var d = Math.log2(-1);

  var x = a + "<br>" + b + "<br>" + c + "<br>" + d;
  document.getElementById("example").innerHTML = x;
}
</script>

</body>
</html>

Output:

Use the Math.log() on different numbers:


Enjoy coding!

Read also:

JavaScript Math Object

JavaScript Math Object Properties

JavaScript Math.ceil()