JavaScript Math log()

The JavaScript Math.log() function returns the natural logarithm of a number.

It returns the natural logarithm (base e) of a number. It is equivalent to ln(x) in mathematics.

The syntax of the Math.log() function is:


log(), being a static method, is called using the Math class name.

Math.log() Parameters

The Math.log() function takes in:

  • x - A number

Return value from Math.log()

  • Returns the natural logarithm (base e) of the given number.
  • Returns NaN for negative numbers and non-numeric arguments.

Example 1: Using Math.log()

// Using Math.log()
var value = Math.log(1);
console.log(value); // 0

var value = Math.log(Math.E);
console.log(value); // 1

var value = Math.log("10");
console.log(value); // 2.302585092994046

var value = Math.log(0);
console.log(value); // -Infinity

var value = Math.log(-1);
console.log(value); // NaN



Example 2: Using Math.log() for other bases

The numerical value for logarithm to any base a from any base b can be calculated with the following change of base identity:

loga(N) = logb(N) / logb(a)

So, we can use Math.log() to calculate the logarithm in any base in the following way:

// find logarithm in any base

function log(base, number) {
  return Math.log(number) / Math.log(base);

// calculating log(100) in base 10
var value = log(10, 100);
console.log(value); // 2

// calculating log(10) in base 5
value = log(5, 10);
console.log(value); // 1.4306765580733933




  • Use the constants Math.LN2 or Math.LN10 for natural log of 2 and 10 respectively.
  • Use the functions Math.log2() or Math.log10() for logarithm base 2 and 10.

Recommended readings: