Last Updated: February 25, 2016
·
404
· kevincennis

Math.log with an optional base

Math.log = (function(){
  var func = Math.log;
  return function(val, base){
    if ( typeof base !== 'number' ) return func(val);
    return func(val) / func(base);
  };
}());

For example, log base 10 of 100:

Math.log(100, 10);

Or, you can still grab the natural log by omitting the base:

Math.log(100);