JavaScript (ES2026) reference · Math Methods and Constants

JavaScript Math.log2() Method

Return the base-2 logarithm of a number, e.g. how many bits or how many halvings are needed.

JavaScript (ES2026) Since ES2015

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

Math.log2(x)

Parameters

ParameterDescription
x A number.

Return value

A number.

Examples

Powers of twoTry it yourself
console.log(Math.log2(8), Math.log2(1024));

Output

3 10
Binary search stepsTry it yourself
const items = 1_000_000;
console.log("binary search steps:", Math.ceil(Math.log2(items)));

Output

binary search steps: 20

Version history

VersionChange
JavaScript ES2015Added

Was this page helpful?