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
Syntax
Math.log2(x)
Parameters
| Parameter | Description |
|---|---|
x |
A number. |
Return value
A number.
Examples
console.log(Math.log2(8), Math.log2(1024));
Output
3 10
const items = 1_000_000;
console.log("binary search steps:", Math.ceil(Math.log2(items)));
Output
binary search steps: 20
Version history
| Version | Change |
|---|---|
| JavaScript ES2015 | Added |
Related
Math.log()· Return the natural logarithm (base e) of a number; divide by Math.log(base) to get a logarithm in any base.Math.log10()· Return the base-10 logarithm of a number, e.g. to count digits or work with decibels.- JavaScript Math · tutorial
- JavaScript Numbers · tutorial