jsguides

Math.LOG2E

Math.LOG2E is a static property of the Math object that represents the base-2 logarithm of E (Euler’s number), approximately 1.4426950408889634. It is a read-only constant; you cannot change its value.

This constant equals log2(e) and is useful when working with logarithmic calculations in base 2, computer science algorithms, and information theory.

Syntax

Math.LOG2E

Math.LOG2E is a property lookup, not a function call. It returns the precomputed value directly from the engine’s constant table, so there is no performance cost to using it repeatedly in tight loops or recursive functions.

Value

Math.LOG2E; // 1.4426950408889634

The returned value is the double-precision floating-point representation of log2(e). Every JavaScript engine stores this to full IEEE 754 precision, so you get the same 16 decimal digits whether you access it in Node.js, Chrome, or Firefox.

Examples

Basic usage

console.log(Math.LOG2E);
// 1.4426950408889634

This is the simplest way to verify the constant. The value never changes across engine versions because it is defined by mathematics rather than an implementation choice.

Relationship with the natural logarithm

Math.LOG2E is the reciprocal of Math.LN2:

console.log(Math.LOG2E === 1 / Math.LN2);
// true

console.log(Math.LOG2E * Math.LN2);
// 1

The second line confirms the inverse relationship numerically. Multiplying the two constants yields exactly 1, which is a useful sanity check when you are debugging code that converts between natural and base-2 logarithms.

Calculating base-2 logarithms

You can use this constant to convert natural logarithms to base-2:

function log2(x) {
  return Math.log(x) * Math.LOG2E;
}

console.log(log2(2));
// 1

console.log(log2(4));
// 2

console.log(log2(8));
// 3

console.log(log2(1024));
// 10

The function multiplies the natural log by the conversion factor. This works because log2(x) = ln(x) × log2(e) for any positive x. Notice that the outputs are exact integers for powers of two — a useful property when you are reasoning about bit shifts or allocation sizes.

Information theory applications

In information theory, Math.LOG2E helps convert between natural entropy and bits:

// Convert natural logarithm entropy to bits
function entropyToBits(natEntropy) {
  return natEntropy * Math.LOG2E;
}

console.log(entropyToBits(0.6931471805599453));
// 1 (approximately 1 bit)

Natural entropy values (in nats) multiplied by LOG2E give you the same information measured in bits. The input 0.693… is ln(2), so the output rounds to 1 bit — exactly one binary decision’s worth of information.

Algorithm analysis

When analyzing algorithms with logarithmic complexity:

// Calculate number of comparisons in binary search
function binarySearchComparisons(n) {
  return Math.ceil(Math.log2(n));
}

console.log(binarySearchComparisons(100));
// 7

console.log(binarySearchComparisons(1000));
// 10

console.log(binarySearchComparisons(1048576));
// 20

Each call computes the ceiling of log2(n) to answer “how many steps does binary search take in the worst case?” The outputs match the familiar rule: doubling the input size adds roughly one comparison.

Bit length calculations

Math.LOG2E is useful for determining how many bits are needed to represent a number:

function bitLength(n) {
  return Math.floor(Math.log2(n)) + 1;
}

console.log(bitLength(1));
// 1

console.log(bitLength(7));
// 3

console.log(bitLength(255));
// 8

console.log(bitLength(1024));
// 11

Floor plus one gives the minimum bit count. Values like 255 (8 bits) and 1024 (11 bits) align with what you would expect from their binary representation lengths.

Common Patterns

Quick base-2 logarithm

A one-liner that avoids the Math.log(x) / Math.LN2 idiom:

// Instead of Math.log(x) / Math.LN2
const quickLog2 = x => Math.log(x) * Math.LOG2E;

console.log(quickLog2(16));
// 4

Multiplication is often faster than division at the hardware level, and the intent — “convert natural log to base-2” — reads more directly than dividing by LN2.

Comparing log bases

A side-by-side comparison of base-2 and base-10 conversions shows the difference in magnitude:

// Relationship between different logarithm bases
const log10 = x => Math.log(x) / Math.LN10;
const log2 = x => Math.log(x) * Math.LOG2E;

console.log(log2(100));
// 6.643856189774724

console.log(log10(100));
// 2

Base-2 logs grow faster than base-10 logs. The ratio between them is constant — log2(100) / log10(100) equals log2(10). The same number spans roughly 6.6 bits but only 2 decimal digits.

Binary-oriented calculations

Math.LOG2E is especially handy when you think in powers of two. File sizes, bit counts, branch depth, and binary search all use base-2 reasoning, so a constant that converts natural logs into log base 2 often fits the math better than a hand-written reciprocal. The code reads more naturally when the value matches the way you explain the formula.

That is also why the constant shows up in information theory and algorithm analysis. Those fields often measure growth in bits rather than decimal digits, so using Math.LOG2E keeps the unit of the calculation visible. It is a small naming choice, but it helps the reader follow what the number represents.

Relationship to Math.LN2

Math.LOG2E and Math.LN2 are inverses in the logarithmic sense. One converts from natural logs to base-2 logs, and the other converts back. If you are writing a helper that moves between natural-space formulas and binary-space formulas, the pair makes the transformation explicit and easy to check.

In practice, that makes the code easier to maintain than burying the conversion in a more complex expression. Someone reading it later can tell whether the formula is about base-2 scaling or just a general logarithm with a conversion factor tacked on.

Why use Math.LOG2E?

Using Math.LOG2E instead of hardcoding 1.4426950408889634 provides several advantages:

  1. Precision — The constant is defined to full floating-point precision by the JavaScript engine
  2. Readability — Code clearly expresses the mathematical concept log2(e)
  3. Standardization — Matches mathematical notation in formulas and algorithms
  4. Efficiency — The value is computed once by the JavaScript engine, not on every calculation

See Also

  • Math.LN2 — the natural logarithm of 2
  • Math.LN10 — the natural logarithm of 10
  • Math.log2() — returns the base-2 logarithm of a number
  • Math.log() — returns the natural logarithm of a number