JavaScript Math.log2() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

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.

JavaScript
Math.log2(x)
CallWhat 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
JavaScript
Math.log2(x)
JavaScript
interface Math {
  log2(x: number): number;
}

Parameters

ParameterDescription
xRequired. A number greater than 0. Zero returns -Infinity. Negative numbers return NaN.

Return Value

Return ValueDescription
numberThe 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.

JavaScript
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
Try It Yourself

How It Works

  1. Powers of two. 2 ** 3 is 8, so the result is 3.
  2. One. 2 ** 0 is 1, so the result is 0.
  3. Fractions. 0.5 is 2 ** -1, so the result is -1.
  4. 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.

JavaScript
function bitsNeeded(values) {
  return Math.ceil(Math.log2(values));
}

console.log(bitsNeeded(256));   // 8
console.log(bitsNeeded(1000));  // 10
console.log(bitsNeeded(5));     // 3
Try It Yourself

How It Works

  1. log2. Math.log2(1000) is about 9.97.
  2. Round up. Math.ceil gives 10 because you cannot use part of a bit.
  3. 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.

JavaScript
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
Try It Yourself

How It Works

  1. Positive only. n > 0 filters out zero and negatives.
  2. Whole number? Number.isInteger checks if the log has no decimals.
  3. Results. 64 is 2 ** 6 (true). 100 is not a power of two (false).

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 support
Math.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.

Next: max()

Learn how to find the largest number.

max() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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