# Extend Math.log to allow for bases != e

DevFeed: [Extend Math.log to allow for bases != e](<https://devfeed.tech/articles/extend-math-log-to-allow-for-bases-e-51946.md>)

Original publisher: [Read original article](<https://lea.verou.me/2009/03/extend-mathlog-to-allow-for-bases-e/>)

Author: Lea Verou

Published: 2009-03-26T00:00:00Z

Content type: tutorial

Language: en

Sources: [Lea Verou's blog](<https://devfeed.tech/sources/lea-verou-s-blog.md>)

Topics: [Mathematics](<https://devfeed.tech/topics/mathematics.md>), [Parameter](<https://devfeed.tech/topics/parameter.md>), [Calculate Pi](<https://devfeed.tech/topics/calculate-pi.md>)

Tags: [base](<https://devfeed.tech/tags/base.md>), [blog](<https://devfeed.tech/tags/blog.md>), [extend](<https://devfeed.tech/tags/extend.md>), [js](<https://devfeed.tech/tags/js.md>), [logarithm](<https://devfeed.tech/tags/logarithm.md>), [math](<https://devfeed.tech/tags/math.md>), [parameter](<https://devfeed.tech/tags/parameter.md>), [tips](<https://devfeed.tech/tags/tips.md>)

## AI overview

The snippet wraps the existing `Math.log` so a second argument can select a base: it returns `log(n) / log(a)`. Without that argument, it returns the natural logarithm, preserving the existing one-argument behavior. The excerpt claims this keeps older scripts working.

## Source excerpt

As Math.log currently stands, it's a bit useless. It only calculates natural logarithms (base e). We can easily modify it however, to calculate logarithms of any base: Math.log = (function() { var log = Math.log; return function(n, a) { return log(n)/(a? log(a) : 1); } })(); We can now specify the base as a second parameter, or still use the default one (Math.E) if we don't specify one, so older scripts won't break or if we want a shortcut to the natural logarithm. ;)