Math.LN2

Holds the natural logarithm of 2, roughly 0.6931471805599453.

Signature
Math.LN2
  • Added in JavaScript ES1

Return value

numberThe closest double-precision value to ln(2), 0.6931471805599453.

Errors

Nothing on read. Assigning to it is silently ignored in sloppy mode and throws a TypeError in strict mode or a module, because the property is non-writable.

Math.LN2 is a static data property holding the natural logarithm of 2. Its historical job is base conversion: before Math.log2() existed, Math.log(x) / Math.LN2 was the only way to take a binary logarithm.

When to reach for it

Two situations. First, half-life and doubling-time arithmetic — a quantity decaying at rate λ halves every Math.LN2 / λ, and one growing at rate r doubles every Math.LN2 / r. Second, information-theory work where Math.LN2 converts nats to bits.

What trips people up

Do not use it to reimplement Math.log2(). The division performs two roundings, so the answer can land just short of an integer for exact powers of two. Math.log(1024) / Math.LN2 happens to give a clean 10 in current engines, but the same pattern with Math.LN10 visibly fails, and nothing in the spec guarantees the base-2 case either. If you then apply Math.floor() — say, to count bits — a value of 9.999999999999998 silently becomes 9. Call Math.log2(), which the engine computes directly.

The property is non-writable, non-enumerable and non-configurable.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Converting a decay constant into a half-life, or a growth rate into a doubling time
  • Expressing an entropy measured in nats as bits
  • Supporting old code that computed base-2 logarithms before Math.log2 existed
  • Estimating how many bits are needed to represent a range of values

Frequently asked questions

Why use Math.LN2 instead of Math.log2()?

Only for formulas where ln(2) is the value you need, such as half-life or doubling-time calculations and nat-to-bit conversion. For a base-2 logarithm call Math.log2(), which is computed directly instead of through two roundings.

How do I calculate a half-life in JavaScript?

Divide Math.LN2 by the decay constant: Math.LN2 / lambda. The same expression with a growth rate gives the doubling time.

What is the value of Math.LN2?

0.6931471805599453, the closest double-precision number to ln(2). It is non-writable, non-enumerable and non-configurable, so it cannot be reassigned.

More examples

Doubling time and half-life

A 7% rate doubles in about 9.90 periods and a 2% rate in about 34.66 — close to the familiar "rule of 70" shortcut. Feeding the doubling time back into continuous growth returns 200 from 100, confirming the formula.

Example · javascript
const doublingTime = (rate) => Math.LN2 / rate;
console.log(doublingTime(0.07)); // 7% growth per period
console.log(doublingTime(0.02));

// Check it: growing for that long doubles the amount
const t = doublingTime(0.07);
console.log(100 * Math.exp(0.07 * t));

Prefer Math.log2 for binary logarithms

For the small powers of two both routes agree exactly, but the pattern is fragile: the division rounds twice and only happens to land on the integer. Math.log2() is a single primitive operation, so it is the one to call when the result feeds a Math.floor or an equality test.

Example · javascript
const values = [1, 2, 4, 8, 1000, 1e6];
for (const v of values) {
  const viaLN2 = Math.log(v) / Math.LN2;
  console.log(v, viaLN2, Math.log2(v), viaLN2 === Math.log2(v));
}

Nats to bits

The fair coin measures 0.6931471805599453 nats, which divided by Math.LN2 is exactly 1 bit. The biased coin comes out at about 0.469 bits, showing how much less information a lopsided outcome carries.

Example · javascript
// Shannon entropy of a fair coin, in nats then bits
const p = [0.5, 0.5];
const nats = -p.reduce((s, q) => s + q * Math.log(q), 0);
console.log(nats);
console.log(nats / Math.LN2); // bits

const biased = [0.9, 0.1];
console.log(-biased.reduce((s, q) => s + q * Math.log(q), 0) / Math.LN2);

Discussion

  • Be the first to comment on this lesson.