Categories
Web development

JavaScript Math.log2()

JavaScript Math.log2()

The JavaScript Math.log2() returns the base-2 logarithm of a number.

Syntax:

Math.log2(number)

Note: A Number, representing the base-2 logarithm of a number.
If the number is -1, -Infinity is returned.
If the number is less than -1, it returns NaN

Example:

<!DOCTYPE html>
<html>
<body>

<button onclick="log2Function()">Try it</button>
<p id="example"></p>

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

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

</body>
</html>

Output:

Return the base 2 logarithm of different numbers:


Enjoy coding!

Read also:

JavaScript Math Object

JavaScript Math.log()

JavaScript Math.log10()