How to Returns the natural logarithm of 10 (approx. 2.302) in JavaScript ?


In JavaScript, we can use the Math.log10() method to return the natural logarithm of 10. The natural logarithm of 10 is approximately 2.302585092994046.

Example:

console.log(Math.log10(10)); // Output: 1
console.log(Math.log10(100)); // Output: 2
console.log(Math.log10(1000)); // Output: 3
console.log(Math.log10(Math.E)); // Output: 0.4342944819032518
console.log(Math.log10(1)); // Output: 0
console.log(Math.log10(0)); // Output: -Infinity
console.log(Math.log10(-1)); // Output: NaN

Alternatively, we can also use the Math.log() method with base 10 as the argument to get the natural logarithm of 10.

Example:

console.log(Math.log(10) / Math.LN10); // Output: 1
console.log(Math.log(100) / Math.LN10); // Output: 2
console.log(Math.log(1000) / Math.LN10); // Output: 3
console.log(Math.log(Math.E) / Math.LN10); // Output: 0.4342944819032518
console.log(Math.log(1) / Math.LN10); // Output: 0
console.log(Math.log(0) / Math.LN10); // Output: -Infinity
console.log(Math.log(-1) / Math.LN10); // Output: NaN


About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.