JavaScript math.exp: How to Calculate e^x

javascript math exp

Math.exp is a built-in JavaScript function that returns the value of e raised to a given number.

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.38905609893065

Here, 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.999999999999996

It 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.83

Examples

Calculate Continuous Growth:

let rate = 0.05;
let years = 10;
let final = 1000 * Math.exp(rate * years);
console.log(final); // 1648.721270700128

This 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.6065306597126334

Here, 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.99999999999999

This 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.6487212707001282

When 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?

Explanation: The 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?

Math.exp(x) calculates e^x. Math.pow(base, exponent) calculates base^exponent. Example:
console.log(Math.exp(2));       // 7.38905609893065
console.log(Math.pow(2, 3));    // 8

Can Math.exp handle negative numbers in JavaScript?

Yes, Math.exp can handle negative numbers. Example:
console.log(Math.exp(-1)); // 0.36787944117144233
console.log(Math.exp(0));  // 1

How to use Math.exp in calculations?

Math.exp is useful in finance, science, and probability calculations. Example:
  • Compound interest: A = P * Math.exp(r * t)
  • Exponential growth: population = initial * Math.exp(rate * time)

Similar Reads

How Does JavaScript Work to Run Code in the Web Browser

Click a button on a website. Something changes. A menu opens or a message pops up. That’s how JavaScript works.…

JavaScript Hello World: Write a Simple Program in 3 Ways

If you are ready to take your first steps into the realm of web development, then there cannot be a…

JavaScript Math trunc: How to Removes Decimals

JavaScript gives you the Math.trunc() function to work with decimal numbers. You use it when you want to remove the…

JavaScript Math.max(): How to Find the Highest Number

Math.max() is a built-in JavaScript function that returns the highest number from a list of values. It has been part…

JavaScript Math ceil: Round Numbers with Precision

Some numbers do not stay whole. You get decimal parts when you divide or calculate prices. You may need to…

JavaScript Math acos Function

The Math.acos function finds the angle in radians from a cosine value in JavaScript. It gives a value between 0…

JavaScript Math.random() Returns Numbers 0–1

Math.random() gives a number between 0 and 1 in JavaScript. It helps create random values for colors or other data.…

What Is AJAX? How It Works in Web Development with Examples

AJAX keeps your web app fast. You do not need to reload the page every time you send or get…

JavaScript do while Loop: How it Works with Examples

The "do while" loop in JavaScript runs code once before checking the condition. Use it when the code must run…

JavaScript Loops and Iteration: How They Work with Examples

You use loops in JavaScript to repeat code without copying it many times. This helps when you handle lots of…

Previous Article

HTML Introduction for Beginners from Scratch

Next Article

PHP array_intersect_key Function: How it Works with Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.