Math.log2()
Returns the base-2 logarithm of a number.
Syntax
Math.log2(x) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
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
| Environment | Since version |
|---|---|
| chrome | 38 |
| firefox | 25 |
| safari | 8 |
| edge | 12 |
| node | 0.12 |