Math.log2()

Returns the base-2 logarithm of a number.

Since ES2015 Spec ↗

Syntax

Math.log2(x)

Parameters

NameTypeRequiredDescription
x number Yes The number whose base-2 logarithm is returned.

Returns

number — log base 2 of x; NaN if x < 0.

Examples

console.log(Math.log2(8));
Output
3
console.log(Math.log2(1024));
Output
10

Notes

Handy for computing bit widths or octave/binary scaling. Returns NaN for negative input and -Infinity for 0.

Browser & runtime support

EnvironmentSince version
chrome 38
firefox 25
safari 8
edge 12
node 0.12

See also