Math.exp is a built-in JavaScript function that returns the value of e raised to a given number.
Table of Content
Here, e is a constant that has a value of about 2.718. This function is part of the Math object, which means you call it as Math.exp(). You cannot use it as a method on numbers.
You can use Math.exp when you work with formulas that involve growth, decay, or other calculations based on the exponential number e.
How the math.exp Works in JavaScript
Math.exp(x) returns e to the power of x. The x can be any real number, positive, negative, or zero.
Here is the basic syntax:
Math.exp(x)x — A number that acts as the exponent.
The result is always a number. If x is NaN, the function returns NaN. If x is positive infinity, the result is positive infinity, and if x is negative infinity, the result is zero.
The function multiplies e by itself x times in a mathematical sense. In reality, it uses an internal algorithm that works for any number, even fractions or negatives. For example, Math.exp(1) returns the constant e, and Math.exp(0) returns 1.
Here is an example:
console.log(Math.exp(2)); // 7.38905609893065Here, the function returns e squared. Since e is about 2.718, squaring it gives about 7.389.
The Math.exp works in all major browsers, including Chrome, Firefox, Safari, Edge, and Opera. It is also available in Node.js, so it can be used in both browser and server environments.
Combine math.exp with Other Math Functions in JavaScript
Math.exp(x) returns e (Euler’s number, about 2.718) raised to the power of x. It becomes more useful when combined with other Math functions to create formulas.
For example, you can use it with Math.log() to work with exponential and logarithmic equations:
console.log(Math.exp(Math.log(20))); // 19.999999999999996It can also work with Math.pow() for more complex models. A common use is in finance for compound interest:
// Continuous compound interest formula: A = P * e^(r * t)
let P = 1000; // principal
let r = 0.05; // interest rate
let t = 3; // years
let A = P * Math.exp(r * t);
console.log(A.toFixed(2)); // 1161.83Examples
Calculate Continuous Growth:
let rate = 0.05;
let years = 10;
let final = 1000 * Math.exp(rate * years);
console.log(final); // 1648.721270700128This code uses Math.exp to find the final value after continuous growth at a rate of 5% per year for 10 years. The number grows faster than with normal interest formulas.
Use Math.exp with Negative Numbers:
let decayRate = -0.1;
let time = 5;
let result = Math.exp(decayRate * time);
console.log(result); // 0.6065306597126334Here, the result shows exponential decay. Since the exponent is negative, the value moves toward zero over time instead of growing.
Link Math.exp with Math.log:
let number = 50;
let logValue = Math.log(number);
let original = Math.exp(logValue);
console.log(original); // 49.99999999999999This example proves that Math.exp can reverse Math.log. The value returned by the log function is the power that Math.exp needs to get the original number.
Calculate e to a Fractional Power:
let fraction = 0.5;
let result = Math.exp(fraction);
console.log(result); // 1.6487212707001282When you pass a fraction to Math.exp, the result is the square root of e. This works the same way as fractional exponents in other math operations.
Wrapping Up
In this article, you learned what Math.exp is and how it works in JavaScript. You also saw how it combines with other Math functions and how to use it in different contexts.
Here is a quick recap:
Math.exp(x)returns e to the power of x.- The result is always a number and works for positive, negative, and fractional values.
- It can be combined with other math functions for advanced formulas.
- It is supported by all major browsers and Node.js.
FAQs
How does JavaScript math.exp work?
Math.exp(x) function returns e raised to the power of x.
Here, e is Euler's number (~2.718).
Example:
let result = Math.exp(2);
console.log(result); // 7.38905609893065
What is the difference between Math.exp and Math.pow in JavaScript?
console.log(Math.exp(2)); // 7.38905609893065
console.log(Math.pow(2, 3)); // 8
Can Math.exp handle negative numbers in JavaScript?
console.log(Math.exp(-1)); // 0.36787944117144233
console.log(Math.exp(0)); // 1
How to use Math.exp in calculations?
- Compound interest: A = P * Math.exp(r * t)
- Exponential growth: population = initial * Math.exp(rate * time)
Similar Reads
You may need to loop through properties when you work with objects in JavaScript. The for...in loop helps you do…
The this keyword links code, context, and object reference in JavaScript. You can use it to refer to the owner…
The Math.acos function finds the angle in radians from a cosine value in JavaScript. It gives a value between 0…
Object constructors and class constructors in JavaScript create objects in a structured way. Both provide methods to build reusable code.…
JavaScript gives you several ways to round numbers, but not all of them round the same way. If you want…
The "do while" loop in JavaScript runs code once before checking the condition. Use it when the code must run…
JavaScript added Math.cbrt to solve cube roots. Before that, developers used custom code or Math.pow with 1/3. It works with…
This guide shows how toSorted function works in JavaScript with arrays. It covers syntax, rules, and examples for numbers, text,…
String Operators in JavaScript help you work with text. You can join, build, or change strings. Most of what you…
JavaScript switch statement checks many values without long chains. It appeared to replace stacked conditions that slow you down. Understand…