Math.E

Holds Euler's number, the base of the natural logarithm, roughly 2.718281828459045.

Signature
Math.E
  • Added in JavaScript ES1

Return value

numberThe closest double-precision value to e, 2.718281828459045.

Errors

Nothing on read. Assigning to it is silently ignored in sloppy mode and throws a TypeError in strict mode or a module, because the property is non-writable.

Math.E is a static data property, not a method. It evaluates to the nearest double to Euler's number e — the base that makes Math.log() the natural logarithm, so Math.log(Math.E) is exactly 1.

When to reach for it

Reach for the constant when e itself appears in your formula as a value: printing it, checking a limit, or writing a decay constant. For raising e to a power, prefer Math.exp(x) over Math.E ** x. Math.exp() is a single correctly-rounded operation, while the exponentiation reads the rounded constant and then rounds again, so the two can disagree in the last bit: Math.E ** 2 gives 7.3890560989306495 and Math.exp(2) gives 7.38905609893065.

What trips people up

Continuous growth and discrete compounding are different models. P * Math.exp(r * t) compounds continuously; P * (1 + r / n) ** (n * t) compounds n times a period, and only converges on the first as n grows. Picking the wrong one produces small, plausible-looking, wrong money.

The property is non-writable, non-enumerable and non-configurable, so it cannot be reassigned.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Modelling continuously compounded interest or continuous population growth
  • Converting a decay constant into a half-life with Math.LN2 / lambda
  • Implementing probability densities such as the normal or exponential distribution
  • Demonstrating that (1 + 1/n)**n converges on e as n grows

Frequently asked questions

Should I use Math.E ** x or Math.exp(x)?

Use Math.exp(x) — it is one correctly-rounded operation, whereas Math.E ** x rounds the constant and then rounds the power, so the results can differ in the final bit (Math.E ** 2 is 7.3890560989306495 while Math.exp(2) is 7.38905609893065).

What is Math.E used for?

It is Euler's number, the base of the natural logarithm, and it appears in continuous growth and decay formulas, probability distributions and anything expressed with ln. Math.log() is the logarithm to this base.

Why is Math.log(Math.E) exactly 1?

Because Math.log() is the natural logarithm — the logarithm to base e — and the implementation returns exactly 1 for the double stored in Math.E. For base 2 or base 10 logarithms use Math.log2() and Math.log10().

More examples

Continuous versus discrete compounding

The three discrete figures climb from 1080 towards the continuous result of about 1083.29 as the compounding period shrinks. The gap between yearly and continuous is over three units on a thousand, which is why the two models must not be swapped for each other in financial code.

Example · javascript
const principal = 1000;
const rate = 0.08;

const discrete = (n) => principal * (1 + rate / n) ** n;
console.log(discrete(1));    // yearly
console.log(discrete(12));   // monthly
console.log(discrete(365));  // daily
console.log(principal * Math.exp(rate)); // continuous

Exponential decay with a rate constant

At t = 0 the full 100 remains; by t = 10 about 60.65 is left. Feeding in ln(2) / λ — roughly 13.86 time units — leaves exactly half, which is how a decay constant and a half-life relate.

Example · javascript
const decay = (amount, lambda, t) => amount * Math.exp(-lambda * t);

console.log(decay(100, 0.05, 0));
console.log(decay(100, 0.05, 10));
console.log(decay(100, 0.05, Math.LN2 / 0.05)); // one half-life
console.log(Math.LN2 / 0.05);

e as the base of the natural logarithm

Math.E and Math.exp(1) are the same double, so the first line is true. log(e³) returns 3, dividing by log(e) is a no-op, and LOG2E multiplied by LN2 comes back as exactly 1 because those two constants are defined as reciprocals.

Example · javascript
console.log(Math.E === Math.exp(1));
console.log(Math.log(Math.E ** 3));
console.log(Math.log(10) / Math.log(Math.E)); // dividing by 1
console.log(Math.LOG2E * Math.LN2); // reciprocal pair

Discussion

  • Be the first to comment on this lesson.