Why This Matters
A logarithmic function asks the question: "what exponent gives me this value?" If 2^10 = 1024, then log base 2 of 1024 is 10. Logarithmic functions are the inverse of exponential functions, and they appear everywhere in computer science (O(log n) algorithms), information theory (entropy measured in bits), and science (pH scale, decibels, Richter scale).
Mastering logarithm properties like the product rule, quotient rule, and power rule lets you simplify complex expressions and solve exponential equations. The change of base formula lets you convert between any two logarithm bases, which is essential when your calculator only has ln and log10 but you need log base 2.
Define Terms
Visual Model
The full process at a glance. Click Start tour to walk through each step.
Logarithms are the inverse of exponentials. Their properties convert multiplication to addition and exponents to multipliers.
Code Example
// Common logarithm bases
console.log("log2(8):", Math.log2(8)); // 3
console.log("log10(1000):", Math.log10(1000)); // 3
console.log("ln(e):", Math.log(Math.E)); // 1
// Change of base: log_b(x) = ln(x) / ln(b)
function logBase(b, x) {
return Math.log(x) / Math.log(b);
}
console.log("log3(81):", logBase(3, 81)); // 4
console.log("log5(125):", logBase(5, 125)); // 3
// Log properties in action
const a = 8, c = 4;
console.log("Product rule:", Math.log2(a * c), "=",
Math.log2(a) + Math.log2(c)); // 5 = 3 + 2
console.log("Quotient rule:", Math.log2(a / c), "=",
Math.log2(a) - Math.log2(c)); // 1 = 3 - 2
console.log("Power rule:", Math.log2(Math.pow(a, 3)), "=",
3 * Math.log2(a)); // 9 = 3 * 3
// Solve exponential equation: 3^x = 100
// Take log of both sides: x * log(3) = log(100)
// x = log(100) / log(3)
const x = Math.log(100) / Math.log(3);
console.log("3^x = 100, x =", x.toFixed(4)); // 4.1918
console.log("verify: 3^x =", Math.pow(3, x).toFixed(2)); // 100.00
// Solving 5 * 2^t = 320
// 2^t = 64, t = log2(64) = 6
const t = Math.log2(320 / 5);
console.log("5 * 2^t = 320, t =", t); // 6Interactive Experiment
Try these exercises:
- Verify the product rule: compute log2(16 * 32) and log2(16) + log2(32). Are they equal?
- Use the change of base formula to compute log7(343) using only Math.log or math.log.
- Solve 2^x = 1000 for x. Check your answer by computing 2^x.
- Solve 500 * (1.06)^t = 2000 for t. This is asking when an investment quadruples at 6% growth.
- Graph y = log2(x) for x from 0.01 to 100 by printing values. Notice how slowly it grows compared to x.
Quick Quiz
Coding Challenge
Write a function called `solveExponential` that solves equations of the form a * b^x = target for x. The formula is: x = log(target / a) / log(b). Return x rounded to 4 decimal places. If the equation has no solution (target/a is not positive, or b is not positive or equals 1), return null.
Real-World Usage
Logarithmic functions appear throughout science and technology:
- Algorithm analysis: O(log n) describes binary search, balanced tree operations, and divide-and-conquer. Log base 2 is the most common in CS.
- Information theory: Entropy is measured using logarithms. The information content of an event with probability p is -log2(p) bits.
- Sound and signals: The decibel scale is logarithmic: dB = 10 * log10(P/P0). This compresses huge dynamic ranges into manageable numbers.
- Chemistry: pH = -log10 of hydrogen ion concentration. Each pH unit represents a tenfold change.
- Earthquake measurement: The Richter scale is logarithmic. A magnitude 7 earthquake is 10 times more powerful than magnitude 6.