Calculate logarithm of a number in JavaScript
This post will discuss how to calculate the logarithm of a number in JavaScript.
There are several ways to calculate the logarithm of a number in JavaScript, depending on the base we want to use. A logarithm is the inverse of an exponential function, which means that it answers the question of what power a given base must be raised to in order to get a given number. For example, the logarithm of 1000 with base 10 is 3, because 10^3 = 1000. Here are some of the methods we can use:
1. Using Math.log() function
The Math.log() is a built-in function that returns the natural logarithm (base e) of a number. We can use this function by calling it with a number as an argument, like this: Math.log(number). The number must not be negative, otherwise the function will return NaN (Not a Number). For example:
|
1 2 3 4 5 6 |
let num = 5; // A number // The natural logarithm of the number let log = Math.log(num); console.log(log); // 1.6094379124341003 |
We can use this function to get the logarithm of any base x by dividing the result by Math.log(x). For example, to get the base 2 logarithm of a number num, we can do like Math.log(num) / Math.log(2).
|
1 2 3 |
let x = 16; let y = Math.log(x) / Math.log(2); console.log(y); // 4 |
2. Using other built-in functions for specific bases
The Math object in JavaScript provides various mathematical functions and constants. There are also some built-in functions in the Math object that return the logarithm of a number in specific bases, such as Math.log2() for base 2, Math.log10() for base 10, and Math.log1p() for base e+1. We can use these functions by passing the number as an argument. The number must be greater than or equal to 0, otherwise these function will return NaN. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
let num = 100; // A number // The logarithm of the number with base 2 let log2 = Math.log2(num); console.log(log2); // 6.643856189774724 // The logarithm of the number with base 10 let log10 = Math.log10(num); console.log(log10); // 2 // The logarithm of the number with base e plus one let log1p = Math.log1p(num); console.log(log1p); // 4.61512051684126 |
3. Using a third-party library
There are many JavaScript libraries that provide mathematical functions, such as math.js, big.js, and decimal.js. We can use these libraries to calculate the logarithm of a number in both Node.js and the browser. For example, using math.js:
|
1 2 3 4 5 6 7 8 9 10 11 |
// Import math.js const math = require('mathjs'); // Calculate the logarithm of a number with any base let num = 8; // A number let base = 3; // A base // The logarithm of the number with the base let log = math.log(num, base); console.log(log); // 1.8927892607143724 |
That’s all about calculating the logarithm of a number in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)