Languages
[Edit]
EN

JavaScript - example, how to calculate logarithm with custom base?

4 points
Created by:
Reilly-Collier
860

In this article, we would like to show how to calculate logarithm for specific base using JavaScript.

Practical example:

// ONLINE-RUNNER:browser;

function calculateLogarithm(base, x) {
	var a = Math.log(x);
    var b = Math.log(base);
  
    return a / b;
}

// Logarithm with custom base:
//                               base    x               y
console.log( calculateLogarithm( 2,      2      ) ); //  1
console.log( calculateLogarithm( 2,      4      ) ); //  2
console.log( calculateLogarithm( Math.E, Math.E ) ); //  1
console.log( calculateLogarithm( 3,      9      ) ); // ~2
console.log( calculateLogarithm( 3,      81     ) ); // ~4
console.log( calculateLogarithm( 10,     10     ) ); //  1

Note: read this article to see Math.log() method documentation.

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

JavaScript - math (popular problems)

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join