Powered by Blogger.

Function Math.log() in JavaScript


The Math.log() function returns the natural logarithm (base e) of a number. The JavaScript Math.log() function is equivalent to ln(x) in mathematics. Below is an example use of  Math.log() function to get base logarithm.

 function getBaseLog(x, y) {  
  return Math.log(y) / Math.log(x);  
 }  
 // 2^4 = 16  
 console.log(getBaseLog(2, 16));  
 // expected output: 4  
 // 10^3  
 console.log(getBaseLog(10, 1000));  
 // expected output: 3  

No comments