JavaScript log2() method

Definition and usage

log2() The method returns the logarithm base 2 of the number.

Example

Return the logarithm base 2 of different numbers:

var a = Math.log2(2.7183);
var b = Math.log2(2);
var c = Math.log2(1);
var d = Math.log2(0);
var e = Math.log2(-1);

Try it yourself

Syntax

Math.log2(x)

Parameter value

Parameter Description
x Required. A number.

Technical details

Return value:

A number representing the logarithm of the number with base 2.

  • If the number is -1, then returns -Infinity.
  • If the number is less than -1, then returns NaN.
JavaScript Version: ECMAScript 2015

Browser Support

Method Chrome Edge Firefox Safari Opera
log2() 38.0 12.0 25.0 8.0 25.0

Related Pages

Tutorial:JavaScript Math