Math.exp()
Math.exp(x) The Math.exp() function returns Euler’s number e raised to the power of x. Euler’s number e is an irrational constant approximately equal to 2.718281828459045. The function is the inverse of Math.log() — if you compute Math.log(Math.exp(x)), you get x back (within floating-point precision).
Exponential growth and decay appear throughout engineering and science: compound interest, radioactive decay, population models, and probability distributions all rely on the natural exponential. Math.exp() is more accurate than Math.pow(Math.E, x) for most inputs because the runtime can use optimized hardware instructions.
Syntax
Math.exp(x)
Parameters
| Parameter | Type | Description |
|---|---|---|
x | number | The exponent to raise e to |
Return value
A positive number representing e^x. Returns 1 for x = 0, returns 0 for x = -Infinity, and returns Infinity for x = Infinity.
Examples
Basic usage
console.log(Math.exp(0));
// 1
console.log(Math.exp(1));
// 2.718281828459045
console.log(Math.exp(2));
// 7.38905609893065
Math.exp(0) is always 1 (any number raised to the power of 0 equals 1), and Math.exp(1) returns Euler’s number itself, approximately 2.71828. These two inputs are the best way to verify that the function is working correctly across different JavaScript engines.
Negative exponents
console.log(Math.exp(-1));
// 0.36787944117144233
console.log(Math.exp(-2));
// 0.1353352832366127
Negative exponents produce values between 0 and 1, modeling decay or diminishing quantities over time. Every negative input pushes the result closer to zero without ever reaching it — the curve approaches the x-axis asymptotically, which is why Math.exp(-Infinity) returns 0 rather than a tiny negative number.
Continuous compound growth
const calculateGrowth = (years) => {
const principal = 1000;
const rate = 0.05; // 5% annual growth
const amount = principal * Math.exp(rate * years);
return amount;
};
console.log(calculateGrowth(10));
// 1648.7212707001282
console.log(calculateGrowth(20));
// 2718.281828459045
Exponential decay
Growth is only half the story. The same exponential function models decay when you flip the sign of the exponent. Radioactive isotopes, cooling objects, and drug metabolism all follow this pattern. The decay constant links the rate of change to the physical half-life of the substance you are modeling.
const calculateDecay = (initial, halfLife, time) => {
const decayConstant = Math.log(2) / halfLife;
return initial * Math.exp(-decayConstant * time);
};
// Carbon-14 half-life is about 5730 years
console.log(calculateDecay(100, 5730, 5730));
// 50
console.log(calculateDecay(100, 5730, 11460));
// 25
The decay formula uses Math.log(2) to compute the decay constant from the half-life. After one half-life, exactly 50% of the original amount remains. The same Math.exp() function that drives decay also powers growth models — a single API covers both directions.
Common patterns
Population growth model
const populationGrowth = (initial, growthRate, time) => {
return initial * Math.exp(growthRate * time);
};
const initialPopulation = 1000;
const growthRate = 0.025; // 2.5% growth per unit time
console.log(populationGrowth(initialPopulation, growthRate, 20));
// 1648.72 organisms (approximately)
Gaussian (normal) distribution
Continuous growth models a single quantity over time. The Gaussian distribution uses Math.exp() differently — to shape a probability curve that peaks at the mean and falls symmetrically. This bell-shaped function appears everywhere from statistical tests to machine learning models like naive Bayes classifiers.
const gaussian = (x, mean, sigma) => {
const coefficient = 1 / (sigma * Math.sqrt(2 * Math.PI));
const exponent = -Math.pow(x - mean, 2) / (2 * Math.pow(sigma, 2));
return coefficient * Math.exp(exponent);
};
console.log(gaussian(0, 0, 1));
// 0.3989422804014327
The Gaussian function’s shape is entirely determined by Math.exp() in the exponent. The probability density is highest at the mean and falls off symmetrically. A related use of Math.exp() is the softmax function, which converts a list of raw scores into a probability distribution — every neural network classifier relies on it to produce normalized output probabilities.
Softmax for machine learning
function softmax(values) {
const exps = values.map(v => Math.exp(v));
const sum = exps.reduce((a, b) => a + b, 0);
return exps.map(e => e / sum);
}
console.log(softmax([1, 2, 3]));
// approximately: [0.090, 0.245, 0.665]
Math.exp() vs Math.pow(Math.E, x)
Both Math.exp(x) and Math.pow(Math.E, x) compute e raised to the power of x. The difference is numerical precision: Math.exp() is more accurate for most inputs because the JavaScript engine can use optimized hardware instructions (such as the x87 FLDL2E + F2XM1 instruction pair) that reduce floating-point rounding errors. Prefer Math.exp() over Math.pow(Math.E, x).
Overflow and underflow
For very large positive inputs, Math.exp() returns Infinity. For very large negative inputs, it returns 0 rather than -Infinity, because e^x approaches zero from the positive side as x → -∞.
Math.exp(710); // Infinity (overflows 64-bit float)
Math.exp(-750); // 0 (underflows to zero)
Math.exp(709.7); // 1.7976... × 10^308 (near max float)
When computing with probabilities or log-likelihoods, overflow is a common issue. Working in log space (using Math.log and summing instead of multiplying) sidesteps most overflow problems.
See Also
- Math.log() — natural logarithm (inverse of exp)
- Math.pow() — raise a number to any power
- Math.log1p() — log(1 + x), more accurate for small x
- Math.sqrt() — square root function