JavaScript log2() method

Definition and usage

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

Example

Return the logarithm of different numbers with base 2:

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. Number.

Technical details

Return value:

Number, representing the logarithm of the number with base 2.

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

Webbläsarstöd

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

Relaterade sidor

Tutorial:JavaScript matematik