23 Oct 2015

How to find the logarithm of a number with specified base

Find the logarithm of a number (y) with specified base (x). logxY

function baseLog(x, y) {
  return Math.log(y) / Math.log(x);
}

Usage:

console.log(baseLog(5, 25)); // 2
console.log(baseLog(7, 49)); // 2
console.log(baseLog(2, 8)); // 3
console.log(baseLog(4, 64)); // 3
console.log(baseLog(2, 16)); // 4