Math.LN2
Added in vES1 · Updated March 14, 2026 · Math
javascript math constants logarithm 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; // 0.6931471805599453
Examples
Basic Usage
console.log(Math.LN2);
// 0.6931471805599453
Calculating Logarithm Base 2
You can calculate log base 2 using the natural logarithm:
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 natural logarithm constant helps convert between different logarithm bases:
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
Math.LN2 is useful when working with binary systems:
// 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
// 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
When analyzing algorithms, Math.LN2 can help with complexity calculations:
// 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
// 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 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
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