Math.LN2
Math.LN2 is a static property of the Math object that represents the natural logarithm of 2, approximately 0.6931471805599453. It is a read-only constant; you cannot change its value.
The natural logarithm of 2 appears frequently in computer science, information theory, and mathematical calculations involving logarithms base 2.
Syntax
Math.LN2
Value
Math.LN2 is one of several mathematical constants built into JavaScript’s Math object. Its value — approximately 0.693 — is the natural logarithm of 2 stored at full double-precision floating point. You cannot reassign it; attempting to do so silently fails in non-strict mode and throws a TypeError in strict mode.
Math.LN2; // 0.6931471805599453
Examples
Basic Usage
Accessing Math.LN2 is straightforward — it behaves like any other read-only property and produces the same value every time. The constant is available in every JavaScript environment, including browsers, Node.js, Deno, and Bun, with no imports or polyfills needed.
console.log(Math.LN2);
// 0.6931471805599453
Calculating logarithm base 2
The natural logarithm provides the bridge to base-2 calculations. Dividing Math.log(x) by Math.LN2 gives log₂(x), which is essential for binary search analysis, tree depth calculations, and information theory. The identity log₂(x) = ln(x) / ln(2) holds for any positive x.
function log2(x) {
return Math.log(x) / Math.LN2;
}
console.log(log2(2));
// 1
console.log(log2(4));
// 2
console.log(log2(8));
// 3
console.log(log2(1024));
// 10
Converting between log bases
The same conversion pattern works for any logarithm base. Dividing the natural log by the natural log of the target base gives the result. For base-10, use Math.LN10; the examples below show both approaches side by side so you can confirm they produce identical values.
function log10(x) {
return Math.log(x) / Math.LN10;
}
function log2(x) {
return Math.log(x) / Math.LN2;
}
console.log(log2(100));
// 6.643856189774724
console.log(Math.log(100) / Math.LN2);
// 6.643856189774724
Binary information calculations
In information theory, the entropy of an event is measured in bits using base-2 logarithms. When probability drops by half, you gain one bit of information. Math.LN2 is the constant that makes these formulas work naturally with JavaScript’s Math.log().
// Calculate entropy in bits
function entropy(probability) {
return -Math.log2(probability);
}
console.log(entropy(0.5));
// 1 bit
console.log(entropy(0.25));
// 2 bits
console.log(entropy(0.125));
// 3 bits
Exponential and logarithmic relationships
Math.LN2 and Math.E are linked by the identity e^(ln(2)) = 2. The two code blocks below verify this relationship in both directions — raising 2 to the power of 1/ln(2) yields e, and taking the natural log of 2 returns the constant itself.
// Verify: 2^(1/ln2) = e
console.log(Math.pow(2, 1 / Math.LN2));
// 2.718281828459045 (approximately Math.E)
// Verify: ln(2^y) = y * ln(2)
console.log(Math.log(2) === Math.LN2);
// true
Algorithm complexity
Binary search and divide-and-conquer algorithms have O(log n) complexity because each step cuts the search space in half. The depth needed to reduce n elements to 1 is ceil(log₂(n)), which Math.LN2 helps compute efficiently. In practice, this tells you the maximum number of comparisons a binary search will ever need for a dataset of a given size.
// Binary search depth needed to find item in sorted array
function binarySearchDepth(n) {
return Math.ceil(Math.log2(n));
}
console.log(binarySearchDepth(100));
// 7
console.log(binarySearchDepth(1000));
// 10
console.log(binarySearchDepth(1048576));
// 20
Common Patterns
Deriving other logarithmic constants
Math.LN2 is a building block for other constants. Since LN10 equals LN2 times log₂(10), you can derive one from the other. The code below verifies this identity numerically using JavaScript’s built-in functions. Knowing this relationship is handy when you only remember one constant and need the other for a quick calculation.
// LN10 = LN2 * log10(2)
console.log(Math.LN10);
// 2.302585092994046
// LN2 * log10(2) = LN10
console.log(Math.LN2 * Math.log10(2));
// 2.302585092994046
Working with powers of 2
If you know the result of raising 2 to some power but need the exponent itself, ln(result) / ln(2) gives the answer. This is a common pattern for reverse-engineering binary size calculations — for example, figuring out how many bits a numeric range needs.
// If 2^x = n, then x = ln(n) / ln(2)
function solveExponent(base, result) {
return Math.log(result) / Math.LN2;
}
console.log(solveExponent(2, 16));
// 4 (because 2^4 = 16)
console.log(solveExponent(2, 1));
// 0 (because 2^0 = 1)
Why use Math.LN2?
Using Math.LN2 instead of hardcoding 0.6931471805599453 provides several advantages:
- Precision — The constant is defined to full floating-point precision
- Readability — Code clearly expresses the mathematical concept
- Standardization — Matches mathematical notation in formulas
- Performance — The value is computed once by the JavaScript engine
Where ln(2) shows up
Math.LN2 appears anywhere a formula needs to convert between exponential growth and base-2 reasoning. That includes binary search depth, compression math, entropy calculations, and decay curves. The constant is especially useful when a derivation naturally starts with natural logarithms but ends in powers of two. Rather than recomputing Math.log(2) every time, the built-in constant keeps the expression short and clearly communicates the math being done.
Math.LN2 compared to Math.log2()
If you only need a base-2 logarithm, Math.log2() is usually the simplest choice. Math.LN2 becomes more useful when you are already working in formulas that use Math.log() or Math.exp(). In those cases, the constant is the conversion factor that ties the expression together. The right choice is mostly about readability: use the direct function when possible, and the constant when it makes the derivation clearer.
Math.LN2 vs Math.log2()
JavaScript provides Math.log2() as a direct function for base-2 logarithms. For most cases, Math.log2() is simpler and more readable than dividing by Math.LN2. The two approaches produce the same result:
Math.log2(1024); // 10
Math.log(1024) / Math.LN2; // 10
// Math.log2 is slightly more accurate for exact powers of 2
// because some engines implement it with a hardware instruction
Math.log2(2 ** 53); // 53 (exact)
Math.log(2 ** 53) / Math.LN2; // may have tiny floating-point error
Use Math.log2() for straightforward log-2 calculations. Math.LN2 is more useful in formulas where you are combining natural logarithms and need the conversion factor.
Half-life and exponential decay
Math.LN2 appears naturally in half-life calculations. The half-life of a quantity is the time it takes to reduce to half its value under exponential decay. The relationship involves ln(2):
// Remaining amount after t time units, given half-life h
function decay(initial, t, halfLife) {
return initial * Math.exp(-Math.LN2 / halfLife * t);
}
decay(100, 5, 10); // ~70.7 — 100 units after 5 time steps, half-life 10
decay(100, 10, 10); // 50 — exactly half after one half-life
decay(100, 20, 10); // 25 — one quarter after two half-lives
Using Math.LN2 directly in this formula is clearer than computing Math.log(2) each time.
See Also
- Math.LN10 — the natural logarithm of 10
- Math.log() — returns the natural logarithm of a number
- Math.log2() — returns the base-2 logarithm of a number