JavaScript Math.log2() Method

Definition
Math.log2(x) returns the base-2 logarithm of a number. It tells you the power of 2 that gives x. For example, Math.log2(8) is 3.
Math is built into JavaScript. Call the method directly. It is not a constructor, so never write new Math().
Syntax
Call Math.log2() like this. Start with this line.
| Call | What it does |
|---|
Math.log2(x) | Returns the base-2 logarithm of x. |
Built into JavaScript. Call Math.log2() on the Math object — no import required.
Library declarations
interface Math {
log2(x: number): number;
}
Parameters
| Parameter | Description |
|---|
| x | Required. A number greater than 0. Zero returns -Infinity. Negative numbers return NaN. |
Return Value
| Return Value | Description |
|---|
| number | The base-2 logarithm of x. Returns -Infinity for 0, and NaN for negative numbers. |
JavaScript Examples
Example 1 — Basic Use
Powers of two give whole numbers.
console.log(Math.log2(8)); // 3
console.log(Math.log2(1024)); // 10
console.log(Math.log2(1)); // 0
console.log(Math.log2(0.5)); // -1
console.log(Math.log2(3)); // 1.584962500721156
3
10
0
-1
1.584962500721156
How It Works
- Powers of two.
2 ** 3 is 8, so the result is 3. - One.
2 ** 0 is 1, so the result is 0. - Fractions.
0.5 is 2 ** -1, so the result is -1. - Other numbers.
3 gives a decimal, about 1.585.
Example 2 — How Many Bits Do I Need?
To store N different values you need ceil(log2(N)) bits.
function bitsNeeded(values) {
return Math.ceil(Math.log2(values));
}
console.log(bitsNeeded(256)); // 8
console.log(bitsNeeded(1000)); // 10
console.log(bitsNeeded(5)); // 3
How It Works
- log2.
Math.log2(1000) is about 9.97. - Round up.
Math.ceil gives 10 because you cannot use part of a bit. - Exact fit.
256 is exactly 2 ** 8, so 8 bits is enough.
Example 3 — Is It a Power of Two?
If log2(n) is a whole number, then n is a power of two.
function isPowerOfTwo(n) {
return n > 0 && Number.isInteger(Math.log2(n));
}
console.log(isPowerOfTwo(64)); // true
console.log(isPowerOfTwo(100)); // false
console.log(isPowerOfTwo(1)); // true
console.log(isPowerOfTwo(0)); // false
How It Works
- Positive only.
n > 0 filters out zero and negatives. - Whole number?
Number.isInteger checks if the log has no decimals. - Results.
64 is 2 ** 6 (true). 100 is not a power of two (false).
Compatibility
Browser & Runtime Support
Math.log2() is part of ES2015 and is Baseline Widely available in modern browsers and Node.js.
✓ Baseline · ES2015
Math.log2()
Supported in Chrome, Firefox, Safari, Edge, and Node.js for years. Safe for modern apps without a polyfill.
98% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full supportMath.log2 Excellent
Bottom line: Use Math.log2() in new code. Only very old browsers, like Internet Explorer, lack it.
Frequently Asked Questions
Math.log2(x) returns the base-2 logarithm of x. Math.log2(8) is 3 and Math.log2(1024) is 10.
Use Math.ceil(Math.log2(N)). For 256 values you need 8 bits.
Use Number.isInteger(Math.log2(n)). It is true for 64 and false for 100.
It returns -Infinity. Math.log2(-1) returns NaN.
It was added in ES2015 and works in all modern browsers and Node.js.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
11 people found this page helpful